我想制作一个遵循这个规则的程序:
给定用户输入“n”,您需要找到从 1 到 n 的所有幸运数字。幸运数字是数字为 4 或 7 或两者兼而有之的数字。
我做了这个,但不知道如何处理大于 10 的数字。
#include<iostream>
using namespace std;
int main()
{
int digits, n, count=0;
cout << "Enter upper limit: ";
cin >> n;
for (int i=0; i<n; i++){
if((i==4) || (i==7)){
count++;
}
}
cout << "Number of lucky numbers in given range is " << count;
return 0;
}
最佳答案
将整数格式化为字符串。然后,测试所需字符的字符串。
奖励方法(仅限整数):您可以使用对数、除法、模数、减法等来遍历并测试每个数字。
关于c++ - 计算幸运数字 - C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30064637/