gpt4 book ai didi

c++ - 输入特定数量的字符

转载 作者:行者123 更新时间:2023-12-02 10:31:07 25 4
gpt4 key购买 nike

我是C++编码的新手,目前正在听在线编码讲座,我必须制作一个程序来计算输入数的除数,并输出*作为除数。
例如)如果我输入8,除数为“1 2 4 8”,而最终输出应为“* ** **** ********”
我已经做到了,所以它可以计算除数,但无法弄清楚下一部分。

#include <stdio.h>
#include <stdlib.h>
int main()
{
int a, b;
scanf("%d", &a);

if(a<0, a>1000)
{
printf("Number must be in the range of 0~1000"); //set the range of the input number
exit(0);
}

for(b =1; b<=a; ++b)
{
if(a%b==0)
printf("%d ", b); //calculate the divisors
}

// the next part of the code which I can't figure out

return 0;
}
我已经做到了,直到这里。

最佳答案

在if语句内部使用另一个for循环,

#include <stdio.h>
#include <stdlib.h>
int main()
{
int a, b,c;
scanf("%d", &a);

if(a<0 || a>1000)
{
printf("Number must be in the range of 0~1000"); //set the range of the input number
exit(0);
}


for(b =1; b<=a; ++b)
{
if(a%b==0){
printf("%d ", b);//calculate the divisors
for(c=0;c<b;c++){ //for loop to print stars
printf("*");
}
}

}

//the next part of the code wich I can't figure out

return 0;
}

关于c++ - 输入特定数量的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62254928/

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