键入特定数量的字符
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:********
相关文章:
- C++字符*缓冲区的大小
- HEX值到wchar_t字符(UTF-8)的转换
- 为什么 Serial.println(<char[]>);返回随机字符?
- 我的字符计数代码计算错误.为什么
- 字符串-C++后显示的随机字符
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 如何在C++中从字符串中分割字符
- 为什么msgrcv()将垃圾字符馈送到缓冲区
- 指向指向字符数组的指针数组的指针
- 如何用转义符替换字符串中的所有特殊字符
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 比较字符数组
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++中无符号字符溢出
- 使用无符号字符数组有效存储内存