我需要编写一个程序来提示一个奇数正整数,并在由星号组成的页面上生成一个菱形;这个数字是高度和宽度。
我不知道在为奇数和正数设定参数之后该怎么办.我不太确定如何使用imut值并使用该值设置菱形图。
我有一个基本的钻石代码,但我不知道它有什么问题。
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int row, col, numstars;
int half, rate = 1;
printf("Please enter a positive odd integer.\n");
scanf("%d", &numstars);
for (row = 1; row <= numstars; row++)
{
half = numstars / 2;
for (col = 0; col < half + 1 - numstars; col++)
printf(" ");
for (col = 1; col <= 2 * numstars - 1; col++)
printf("*");
if ((numstars == (half + 1)))
rate = -rate;
numstars = numstars + rate;
printf("\n");
}
return 0;
}发布于 2016-09-29 13:21:59
你让我们在这里教你算法的基本知识。
最大的问题是,在numstar循环中修改for .所以你得到一个无限循环..。
下面是我将如何修复您的代码:使其简单并区分两种情况
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int row, col, numstars;
int half, rate=1;
printf("Please enter a positive odd integer.\n");
scanf("%d", &numstars);
half=(numstars-1)/2;
for (row=0; row<numstars;row++)
{
if(row <= half) {
for (col=0; col< half-row; ++col) printf(" ");
for ( ; col<= half+row; ++col) printf("*");
} else {
for (col=0; col< row-half; ++col) printf(" ");
for ( ; col< numstars+half-row; ++col) printf("*");
}
printf("\n");
}
return 0;
}https://stackoverflow.com/questions/39713416
复制相似问题