如何在c++中使用int值创建循环(简单)
How can I create loop using int value in c++ (simple)
我有一个整数值,例如5794。我想在每个数字上应用几个计算而不使用数组
int encrypt;
cout << "Enter 4 digit numbers to encryption :> ";
cin >> encrypt;
for(int i=1;i<=encrypt;i++){
cout << encrypt << endl;
}
这段代码打印整个数字,但是我想打印每个数字而不使用数组。如果号码是5794,我想显示:
5
7
9
4
我不希望它像这样显示:
5794
提示:使用modulus
(%)和division
(/)运算符
如果给定的数字有四个digit
,则将其除以1000,100,10,1:
std::cout << "n" << encrypt/1000%10;
std::cout << "n" << encrypt/100%10;
好吧,如果你有数字5794,你需要从数学上提取这些数字。
提示:如果将数字除以1000会发生什么?
扫描数字,然后可以使用整数除法和提醒(%)运算符提取每个数字
这是你想要的吗?:
// get the number from stdin, std::cin >> v;
visitNumber(int v)
{
int v1 = v;
size_t len = 0;
while(v1){
v1 /= 10;
++ len;
}
for(int i=len; i>0; --i){
std::cout << (v/pow(10,i)) << std:endl; // or some other op you favorate
}
}
既然你有麻烦把其他回复者的建议写进代码:
int divisor = 1000;
std::cout << "enCRAPtion digits:n";
for (unsigned int j = 0; j < 3; ++j)
{
cout << (encrypt / divisor) << "nn";
encrypt = encrypt % divisor;
divisor = divisor / 10;
}
顺便说一下,这个词是"加密"而不是"加密"。
相关文章:
- 使用std::list创建循环链表
- 如何创建循环来遍历数组
- 如何将 A 实现到 B 中,将 B 实现到 A 中(不创建循环)?
- 如何创建循环数组
- 有没有办法在 c++ 中创建循环数组
- 通过创建循环简化一些重复操作的代码
- 如何为创建循环创建新样式
- 尝试使用结构在C++中创建循环缓冲区
- 创建循环功能以计算成本
- 如何在C++中使用 STL 列表创建循环
- 为无效字符创建循环
- 在链表创建循环中插入
- C++:创建循环条件的 int 数组时出现问题
- boost图形从txt文件创建循环中的图形
- 如何在omnet++中创建循环
- c++为不符合条件的数据输入创建循环
- 如何在c++中使用int值创建循环(简单)
- 路径查找算法创建循环
- 需要帮助创建循环
- 如何在php中创建循环模板