UVa485 - Pascal's Triangle of Death

#include 
#include 

#define MAX 300
char a[MAX][MAX][MAX];
void reverse(char *from, char *to);
void call_sum(char *first, char *second, char *result);
int main()
{
 char res[MAX];
 int flag = 1;
 int count, i, len;
 strcpy(a[0][0], "1");
 strcpy(a[1][0], "1");
 strcpy(a[1][1], "1");
 printf("1\n1 1\n");
 count = 2;
 while (flag) {
  strcpy(a[count][0], "1");
  strcpy(a[count][count], "1");
  for (i = 1; i < count; i++) {
   call_sum(a[count - 1][i - 1], a[count - 1][i], res);
   len = strlen(res);
   strcpy(a[count][i], res);
   if (len >= 61)
    flag = 0;
  }
  for (i = 0; i <= count; i++) {
   printf("%s", a[count][i]);
   if (i < count)
    printf(" ");
   else
    printf("\n");
  }
  count++;
 }

 return 0;
}

void reverse(char *from, char *to)
{
 int len = strlen(from);
 int i;
 for (i = 0; i < len; i++) {
  to[i] = from[len - 1 - i];
 to[len] = '\0';
 }
}
void call_sum(char *first, char *second, char *result)
{
 char F[MAX], S[MAX], Res[MAX];
 int f, s, sum, extra, now;
 f = strlen(first);
 s = strlen(second);
 reverse(first, F);
 reverse(second, S);
 for (now = 0, extra = 0; now < f && now < s; now++) {
  sum = (F[now] - '0') + (S[now] - '0') + extra;
  Res[now] = sum % 10 + '0';
  extra = sum / 10;
 }
 for (; now  < f; now++) {
  sum = F[now] + extra - '0';
  Res[now] = sum % 10 + '0';
  extra = sum / 10;
 }
 for (; now < s; now++) {
  sum = S[now] + extra -'0';
  Res[now] = sum % 10 + '0';
  extra = sum / 10;
 }
 if (extra)
  Res[now++] = extra + '0';
 Res[now] = '\0';
 if (strlen(Res) == 0)
  strcpy(Res, "0");
 reverse(Res, result);
}

你可能感兴趣的:(#,uva)