题解 | #水仙花数#
水仙花数
https://www.nowcoder.com/practice/11c9f023a9f84418a15b48792a5f7c70
看咱们的操作!
#include <bits/stdc++.h> using namespace std; int main(){ int m, n, flag; //扫描输入 while(cin >> m >> n){ //初始化标志为0 flag = 0; //开始循环m-n范围内的数 for(int i = m; i <= n; i++){ int tmp = i, dev = i, ans = 0; //对范围内的每个数进行判断 while(tmp != 0){ dev %= 10; ans += dev * dev * dev; tmp /= 10; dev = tmp; } //对得到的ans判断,是否满足水仙花数 if(ans == i){ flag = 1; //标志为1 cout << i << " "; } } //如果范围内没有数满足水仙花数,也即flag没有置为1,输出no if(flag == 0){ cout << "no" << endl; }else{ cout << endl; } } return 0; }