创建一个子请求数字更大的数字:C++
Creating a number where subsequest digits are greater : C++
我必须写一个函数说numbers(int MSD,int num)
这样,如果我调用该函数作为numbers(5,4)
,它应该生成所有大于 5000 的 4 位数字,其中后续数字更大。
例如
它应该输出
5678
5679
5689
5789
6789
这是唯一可能的一组 4 位数字,其中后续数字大于前一个数字。
相似性numbers(3,3)
应输出:
345
346
347
348
349
356
357
358
359
...
...
456
457
...
789
希望我把我的问题说清楚。尝试了很多,但我无法实现逻辑。
谢谢
你可以使用递归...(或将下面的代码转换为迭代代码)在这个例子中,我打印到标准输出,但你可以用生成的数字做任何你想做的事情。
我修复了代码以匹配您的确切请求,并添加了 PoW 的朴素实现。
int Pow(int a, int b)
{
int res = a;
for (int i = 0 ; i < b-1 ; ++i)
{
res *= a;
}
return res;
}
void numbersInternal(int MSD,int num,int _base)
{
if (num == 1)
{
for (int j = MSD ; j <= 9 ; ++j)
{
cout << _base + j << endl;
}
}
else
{
for (int j = MSD ; j <= 9-num+1 ; ++j)
{
numbersInternal(j + 1,num-1,_base + Pow(10,num-1)*j);
}
}
}
void numbers(int MSD,int num)
{
numbersInternal(MSD,num,0);
}
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 检查输入是否不是整数或数字
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 如何用数字处理log(0)
- 最高有效数字侧的第N位
- 如何获取一个数字的前3位
- 查找最接近的大于当前数字的数字的索引
- 找到两对数字,使它们的乘积的绝对差最小化
- 我想做一个彼此不同但重复出现的数字
- 将数字转换为字母(例如:123 转换为一二三)
- C++如何计算用户输入的数字中的偶数位数
- 如何在C++中确定文本文件中的元素是字符还是数字
- 打印数字图案
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 如何检查一个c++字符串中有多少相同的字符/数字
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 将数字打印成文字
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 在将数字随机生成为数组期间从内存输出随机数的数组