题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2010
题解:
先求出一个三位数的每一位再进行判断即可,也可以先把100到999内的所有水仙花数求出来,进行范围的输出。
AC代码:
#include <cstring>
#include <iostream>
using namespace std;
bool IsAns(int n) {
int a = n % 10, b = n / 10 % 10, c = n / 100;
if(a * a * a + b * b * b + c * c * c == n)
return true;
return false;
}
int main() {
int n, m, k;
int ans[10];
while (cin >> n >> m) {
k = 0;
memset(ans, 0, 10);
for(int i = n; i <= m; i++) {
if(IsAns(i)){
ans[k++] = i;
}
}
for(int i = 0; i < k; i++) {
if(i != 0){
cout << " ";
}
cout << ans[i];
}
if(k == 0)
cout << "no";
cout << endl;
}
return 0;
}