Floyd的三角形是一个连续数字的直角三角形,从左上角的1开始-
例如,
1 2 3 4 5 6 7 8 9 10
#include <stdio.h>
int main(){
int rows, i,j, start = 1;
printf("Enter no of rows of Floyd's triangle :");
scanf("%d", &rows);
for (i = 1; i <= rows; i++){
for (j = 1; j <= i; j++){
printf("%d ", start);
start++;
}
printf("\n");
}
return 0;
}输出结果Enter no of rows of Floyd's triangle :6 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
以下程序显示了如何反转弗洛伊德三角形-
#include<stdio.h>
int main() {
int num, i, j;
printf("输入行数: ");
scanf("%d",&num);
int k = num*(num+1)/2;
for(i=num; i>=0; i--) {
for(j=1; j<=i; j++)
printf("%4d",k--);
printf("\n");
}
return 0;
}输出结果输入行数: 7 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1