gpt4 book ai didi

读入一个值并将其用作钻石参数的 C 程序

转载 作者:太空宇宙 更新时间:2023-11-04 00:01:58 25 4
gpt4 key购买 nike

我需要编写一个程序来提示输入一个奇数正整数,并在由星号组成的页面上产生一个菱形;数字是高度也是宽度...

我不确定在设置奇数和正数参数后要做什么...我不太确定如何使用 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;
}

最佳答案

你要我们在这里教你算法的基础知识。

  • 你必须更具体地说明你遇到的问题
  • 您必须学习如何调试 - Internet 上有大量资源,但最简单的方法是从一本入门书籍开始。

最大的问题是,您在 for 循环中修改了 numstar...所以您得到了一个无限循环...

以下是我将如何修复您的代码:使其简单并区分两种情况

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

关于读入一个值并将其用作钻石参数的 C 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39713416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com