暑期多校联合比赛F题 8_24 杨辉三角的打印

来源:互联网 发布:2017淘宝搜索规则变化 编辑:程序博客网 时间:2024/05/22 03:13

F、Pascal'sTriangle of DeathTime                 limit: 1.000 seconds

In this problem, you are asked to generate Pascal's Triangle.
Pascal's Triangle is useful in many areas from probability to
polynomials to programming contests. It is a triangle of integers
with ``1'' on top and down the sides. Any number in the interior
equals the sum of the two numbers above it. For example, here are
the first 5 rows of the triangle.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
In ``Pascal's Triangle of Death,'' you are to generate a left
justified Pascal's Triangle. When any number in the triangle is
exceeds or equals 10^60, your program should finish printing the
current row and exit. The output should have each row of the
triangle on a separate line with one space between each element.
The final element of each line should be directly followed by a
newline. There is no space after the last number on each line.


Sample Input
There is no input for this problem.

Sample Output
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
.
.
.
etc.

题目大意:打印杨辉三角直到遇到一个等于或者大于10^60的数(当然还有接着打印完这一行),一开始被这大数吓倒了,尼玛可恶啊!

思路:其实我就卡在数学的杨辉三角没学好,没算到大约205层就可以到10^60这个数了,

还有并不知道三维数组竟然可以开到那么大,我勒个去啊,对不起队友羽哥还有杨烁啊 。

 

 

 

program:

 

#include <stdio.h> 
#include <string.h> 
 
#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';//去掉前导0 
 if (strlen(Res) == 0) 
    strcpy(Res, "0"); 
 reverse(Res, result); 

原创粉丝点击