我正在尝试为给定的 n 个变量获取这样的输出。如果 n=5,输出应如下:
*
***
*****
*******
*********
在我的代码中,当我打印时,输出会反转并且没有空格。我尝试了很多方法来实现这一目标,但找不到解决方案。我收到的 n=5 的输出是:
*********
*******
*****
***
*
代码如下:
#include <stdio.h>
int main() {
int n=0;
int b=0;
puts("Please Enter an Integer");
scanf("%d",&n);
for(n;n>0;n--){
b = 2*n - 1;
for(b;b>0;b--){
printf("*");
}
printf("\n");
}
return 0;
}
最佳答案
经过一番激烈的头脑 Storm ,我终于找到了解决方案。
#include <stdio.h>
int main() {
int n=0;
int a=0;
int b=1;
int c=0;
puts("Please Enter an Integer");
scanf("%d",&n);
for(n;n>0;n--){ // Loop indicating that the pattern will run n times.
c=n-1; // Loop for the spaces to align the pattern to the centre.
for(c;c>0;c--){
printf(" ");
}
for(a;a<b;a++){ // Loop for the pattern.
printf("*");
}
printf("\n");
b=b+2;
a=0;
}
return 0;
}
关于c - 关于打印连续图案的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53143793/