数据结构杨辉三角
#include
typedef struct
{
int *data;
int front;
int rear;
}
sqqueue;
int main()
{
int i, j, m, s1 = 0, s2 = 1;
sqqueue q;
system("cls");
q.data = (int *)malloc(100 * sizeof(int));
q.rear = q.front = 0;
q.data[q.rear] = s2;
q.rear++;
printf("%40d", s2);
for (i = 2; i <= 8; i++)
{
s1 = 0;
for (m = 1; m <= 40 - i; m++)
printf("%c", ' ');
for (j = 1; j <= i - 1; j++)
{
s2 = q.data[q.front];
q.front++;
printf("%d ", s2);
q.data[q.rear] = s1 + s2;
q.rear++;
s1 = s2;
}
printf("%d\n", 1);
q.data[q.rear] = 1 + s2;
q.rear++;
}
}
怎么让第二行的两个1在应该在的位置啊?
------解决思路----------------------
最简单的办法:
printf("%40d\n", s2); // 加个换行即可
------解决思路----------------------
仅供参考:#include
#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
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
#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
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