在C中绘制一个垂直金字塔(Drawing a Vertical Pyramid in C)
我必须得到如下所需的输出:
1 2 6 3 7 10 4 8 11 13 5 9 12 14 15但我似乎无法弄清楚如何做到这一点。 我得到的只是:
1 2 6 3 7 6 4 8 7 6 5 9 8 7 6这是我的代码:
#include<stdio.h> int main() { int n,i,j; scanf("%d",&n); for(i=1;i<=n;i++) { for(j=0;j<i;j++) { if((j+1)==1) printf("%d ",i); else printf("%d ",i+n-j); } printf("\n"); } return 0; }但我理解了所需的输出:我必须按照直角三角形的顺序从1到15打印数字。
I have to get the desired output as follows:
1 2 6 3 7 10 4 8 11 13 5 9 12 14 15But I can't seem to figure out how to do it. All I get is:
1 2 6 3 7 6 4 8 7 6 5 9 8 7 6Here is my code:
#include<stdio.h> int main() { int n,i,j; scanf("%d",&n); for(i=1;i<=n;i++) { for(j=0;j<i;j++) { if((j+1)==1) printf("%d ",i); else printf("%d ",i+n-j); } printf("\n"); } return 0; }But I understood the desired output: I have to print the numbers from 1 to 15 in ascending order like a right angled triangle.
最满意答案
#include <stdio.h> #define SZ 5 int main() { int i,j, add = SZ, val[SZ+1] = {1}; for( j=1; j<=SZ; ++j ) { for( i=0; i<j; ++i ) printf( "%2d ", val[i]++ ); printf( "\n" ); val[j] = val[j-1] + --add; } } #include <stdio.h> #define SZ 5 int main() { int i,j, add = SZ, val[SZ+1] = {1}; for( j=1; j<=SZ; ++j ) { for( i=0; i<j; ++i ) printf( "%2d ", val[i]++ ); printf( "\n" ); val[j] = val[j-1] + --add; } }更多推荐
发布评论