如何在这个程序中使用模数将数字变为字母?
How does one change a number to letter using a modulus in this program?
原来,代码输出组合而不替换。它可以在没有替换的组合中找到。c++ I添加了当集合完成时从集合中减去1的部分。例如,如果输入集合{0,1,2,3,4}中的4的组合,则输出将如下所示:
梳子(N=选择的物品,K=设定的数量)
梳(N = 5, K = 4)~输出~
0,1,2,3
0,1,2,4
0,1,3,4
0,2,3,4
1,2,3,4
⇓梳子(N = 5, K = 4:1)~输出~
0,1,2
0,1,3
0,1,4
0,2,3
0,2,4
0,3,4
1,2,3
1,2,4
1,3,4
2,3,4
⇓梳子(N=5,K=(4-2)或(3-1))~输出~
0,1
0,2
0,3
0,4
1,2
1,3
1,4
2,3
2,4
3,4
⇓梳子(5,K=(4-3)或(2-1))~输出~
结束如果一个人想把数字变成字母,而数字的顺序是相同的,那么如何用模数来做到这一点呢?关于如何做到这一点,还有其他的想法吗?
我的尝试如下。
#include <iostream>
#include <string>
using namespace std;
void comb(int N, int K)
{
std::string bitmask(K, 1);
bitmask.resize(N, 0);
do {
for (int i = 0; i < N; ++i)
{
if (bitmask[i])
std::cout << " " << i;
if (i == 0 || i%10 == 0)
std::cout << " " << "C" << endl;
else if (i == 1 || i%10 == 1)
std::cout << " " << "C#" << endl;
else if (i == 2 || i%10 ==1)
std::cout << " " << "D" << endl;
//the "else if" statements go up to i == 11 which is B//
}
} while (std::prev_permutation(bitmask.begin(), bitmask.end()));
}
int main()
{
for (int i = 6; i > 0; i--){
comb(6,i);
}
}
输出不太令人满意。
Sound
0C
1C#
2D
3D#
Sound
0C
1C#
2D
D#
0C
1C#
2D
D#
0C
...
将0
到n
的整数映射到另一种类型的经典方法是使用数组。
const std::string string_map[] =
{
"C", // instead of 0
"C#", // instead of 1
"D", // instead of 2
....
}
然后在同一代码中,用string_map[n]
代替整数n
…
相关文章:
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 用于检查数字是否有数字或可被该数字的值整除的程序
- 程序以使用 C++ 中的 while 循环查找一组数字的最小值
- 为什么有时我输入一个整数,程序将第一个输入的数字打印成十进制数?
- 为什么当我输入较大的数字时,我的程序会到达文件末尾?
- 如何在C++程序中使用下标数字?
- 为什么使用数组元素查找最大数字的程序不起作用?
- 如何在 WindowProc 处理程序中区分箭头键和数字键盘?
- 在 txt 文件中显示前两个数字的程序
- 在不添加进位的情况下添加数字的C++程序
- 为什么我的程序在读取/写入文件时会删除最重要的数字?
- 一个C++程序,用于在输入位数时输出具有特定位数的 .txt 文件中的所有数字
- 每次运行此 c++ 程序时如何获得不同的数字?
- 程序 查找数字的重复
- 反转一个数字程序不起作用,为什么?
- 为什么这个程序接受整数和字母数字输入并打印它们? C++ 中的 std::string 也采用整数值吗?
- 如何制作一个查找数字平方值的 c++ 程序
- 双精度数字程序意外值
- For循环-魔术数字程序
- 完全数字程序中的逻辑错误