用C语言如何打印这种三角形?
0
1 2
3 4 5
6 7 8 9
10 11 12 13 14
要求:读一个数字并且把它保存在变量行里。根据行数来打印。这是输入5也就是5行的例子。
谢谢各位了!
[解决办法]
- C/C++ code
int getfirst(int row) // 获得第row行第一列的数字,row从0开始计数{ if(0 == row) return 0; return getfirst(row-1)+row;}int main(void){ int row; printf("Input the row number\n"); scanf("%d", &row); for(int i=0; i<row; i++) { for(int j=0; j<=i; j++) { printf("%d\t", j+getfirst(i)); } printf("\n"); } return 0;}
[解决办法]
#include "stdlib.h"
void main()
{
int a,i,count=0,j;
printf("input lines number: ");
scanf("%d",&a);
for(i=0; i<a;i++)
{
for(j=0;j<=i;j++)
{
printf("%4d",count++);
}
printf("\n");
}
printf("\n");
system("pause");
}