检查数字是否是C ++中的神秘数字
在这里,我们将看到如何检查数字是否为神秘数字。神秘数字是可以由两个数字之和表示的数字,并且数字彼此相反。让我们看一下代码以获得更好的主意。我们必须检查所有对并找到决定。
示例
#include <bits/stdc++.h>
using namespace std;
int revNum(int str) {
string s = to_string(str);
reverse(s.begin(), s.end());
stringstream ss(s);
int rev = 0;
ss >> rev;
return rev;
}
bool isMysteryNumber(int n) {
for (int i=1; i <= n/2; i++) {
int j = revNum(i);
if (i + j == n) {
cout << i << " " << j;
return true;
}
}
return false;
}
int main() {
int num = 121;
if(isMysteryNumber(num)){
cout << "\n" << num << " is a Mystery number";
}else{
cout << " is not a Mystery number";
}
}输出结果
29 92 121 is a Mystery number
热门推荐
10 餐饮聚餐祝福语简短精辟
11 关于上岸成功祝福语简短
12 清晨寄语诗句祝福语简短
13 感谢室友结婚祝福语简短
14 彩礼转账祝福语大全简短
15 升职贺卡祝福语简短精辟
16 端午酒桌祝福语简短
17 伴娘前夫结婚祝福语简短
18 阿姨退休蛋糕祝福语简短