键入特定数量的字符

type specific amount of characters

本文关键字:字符      更新时间:2023-10-16

我是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;
}
for(b =1; b<=a; ++b)
{
if(a%b==0){
// printf("%d ", b); //calculate the divisors
for ( int i = 0; i < b; i++ ){
printf("*");
}
printf(" ");
}
}

你计算除数,即 b

如果输入为 8,则 b 将为 1 2 4 8

因此,您可以将 B 用作 for 循环停止条件。

然后,输出为:

b = 1:*

b = 2:**

b = 4:****

b = 8:********