# 小弟欲表示“杨辉三角“，求助！！

swjtu_kzh 2013-02-28 01:00:33

...全文
162 点赞 收藏 10

10 条回复

swjtu_kzh 2013-02-28

swjtu_kzh 2013-02-28

``````
int main()
{
int i, j, a[100][100], n;
n = 5;
a[0][0] = a[1][0] = a[1][1] = 1;
printf("%2d\n%2d%2d\n", a[0][0], a[1][0], a[1][1]);
for (i = 2; i < n; i++)
{
a[i][0] = a[i][i] = 1;// 应该是a[i][i]=1，而不是 a[i][n-1]=1
printf("%2d", a[i][0]);
for (j = 1; j < i; j++)// j应该是到 < i，而不是 < n-1
{
a[i][j] = a[i-1][j] + a[i-1][j-1];
printf("%2d", a[i][j]);
}
printf("%2d\n", a[i][i]); // 应该是 a[i][i]，而不是a[i][n-1]
}
}
``````

swjtu_kzh 2013-02-28

swjtu_kzh 2013-02-28

swjtu_kzh 2013-02-28

``````#include <stdio.h>
#define MAXN 10
unsigned __int64 y[MAXN][MAXN];
int i,j;
char s[60];
void main() {
printf("%30s\n","1  ");
printf("%33s\n","1     1  ");
y[1][0]=1ui64;y[1][1]=1ui64;
for (i=2;i<MAXN;i++) {
y[i][0]=1ui64;sprintf(s,"1  ");
for (j=1;j<=i-1;j++) {
y[i][j]=y[i-1][j-1]+y[i-1][j];
sprintf(s,"%s   %-3I64u",s,y[i][j]);
}
y[i][i]=1ui64;sprintf(s,"%s   1  ",s);
printf("%*s\n",30+i*3,s);
}
}
//                           1
//                        1     1
//                     1     2     1
//                  1     3     3     1
//               1     4     6     4     1
//            1     5     10    10    5     1
//         1     6     15    20    15    6     1
//      1     7     21    35    35    21    7     1
//   1     8     28    56    70    56    28    8     1
//1     9     36    84    126   126   84    36    9     1
``````
``````//按如下格式打印杨辉三角形的前MAXN行。
//  1
//  1 1
//  1 2 1
//  1 3 3 1
//  1 4 6 4 1
//  1 5 10 10 5 1
#include <stdio.h>
#define MAXN 68
unsigned __int64 y[MAXN][MAXN];
int i,j;
void main() {
printf("1\n");
printf("1 1\n");
y[1][0]=1ui64;y[1][1]=1ui64;
for (i=2;i<MAXN;i++) {
y[i][0]=1ui64;printf("1 ");
for (j=1;j<=i-1;j++) {
y[i][j]=y[i-1][j-1]+y[i-1][j];
printf("%I64u ",y[i][j]);
}
y[i][i]=1ui64;printf("1\n");
}
}
//1
//1 1
//1 2 1
//1 3 3 1
//1 4 6 4 1
//1 5 10 10 5 1
//1 6 15 20 15 6 1
//1 7 21 35 35 21 7 1
//1 8 28 56 70 56 28 8 1
//1 9 36 84 126 126 84 36 9 1
//...
//1 67 2211 47905 766480 9657648 99795696 869648208 6522361560 42757703560 247994680648 1285063345176 5996962277488 25371763481680 97862516286480 345780890878896 1123787895356412 3371363686069236 9364899127970100 24151581961607100 57963796707857040 129728497393775280 271250494550621040 530707489338171600 972963730453314600 1673497616379701112 2703342303382594104 4105075349580976232 5864393356544251760 7886597962249166160 9989690752182277136 11923179284862717872 13413576695470557606 14226520737620288370 14226520737620288370 13413576695470557606 11923179284862717872 9989690752182277136 7886597962249166160 5864393356544251760 4105075349580976232 2703342303382594104 1673497616379701112 972963730453314600 530707489338171600 271250494550621040 129728497393775280 57963796707857040 24151581961607100 9364899127970100 3371363686069236 1123787895356412 345780890878896 97862516286480 25371763481680 5996962277488 1285063345176 247994680648 42757703560 6522361560 869648208 99795696 9657648 766480 47905 2211 67 1
``````

Gonefar 2013-02-28

q422013 2013-02-28

C语言

6.4w+

C语言相关问题讨论

2013-02-28 01:00