为什么纹关公的人要死:一个简单的c++题,请高手指教。

来源:百度文库 编辑:神马品牌网 时间:2024/05/04 12:47:02
main ()
{
int row, col, limit;
printf("Please enter a number between 1 and 9: ");
scanf("%d", &limit);

for (row=1; row<=limit; row++)
{
for (col =1; col <= limit; col++)
if (col >= row)
printf ("%d", col);

printf ("\n");
}
}
这个程序如果输入9,显示的是
123456789
23456789
3456789
456789
56789
6789
789
89
9
如果我想显示为
123456789
12345678
1234567
123456
12345
1234
123
12
1
该怎么改这个程序。谢谢。

main ()
{
int row, col, limit;
printf("Please enter a number between 1 and 9: ");
scanf("%d", &limit);

for (row=limit; row>0; row--)
{
for (col =1; col <= row; col++)

printf ("%d", col);

printf ("\n");
}
}

main ()
{
int row, col, limit;
printf("Please enter a number between 1 and 9: ");
scanf("%d", &limit);

for (row=limit; row>0; row--)
{
for (col =1; col <= limit; col++)
if (col >= row) printf ("%d", col);
printf ("\n");
}
}

通过观察可以看到 第N行的范围为1到limit-row+1
比如第一行: 是1到9 (9-1+1)

程序改为:
for (row=1; row<=limit; row++)
{
for (col =1; col <= limit-row+1; col++)
{
printf ("%d", col);
printf ("\n");
}
}

limit
把这个改成for 循环吧 初始值改为9
自己再琢磨琢磨`