在C中绘制一个垂直金字塔(Drawing a Vertical Pyramid in C)

编程入门 行业动态 更新时间:2024-10-28 02:21:08
在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 15

But 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 6

Here 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; } }

更多推荐

本文发布于:2023-07-15 11:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1112849.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:金字塔   Drawing   Pyramid   Vertical

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!