水仙花数

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
bool is_daffodil(int n)
{
int a, b, c; // 百位, 十位, 个位
a = n / 100;
b = n / 10 % 10;
c = n % 10;
if(a*a*a + b*b*b + c*c*c == n) return true;
else return false;
}
int main()
{
int m, n;
while(~scanf("%d %d", &m, &n))
{
int count = 0;
for(int i = m; i <= n; ++ i)
{
if(is_daffodil(i))
{
if(count > 0) printf(" ");
printf("%d", i);
count ++;
}
}
if(count == 0) printf("no");
printf("\n");
}
return 0;
}
本题有两个坑点(对我来说, 哈哈):
(1) 取三位数的个位不是用n%100, 而是n%10
(2) 输出格式, 严格按照最后一个数后面没有空格

浙公网安备 33010602011771号