题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2010
第一种解法:
#include<stdio.h>
int main()
{
int b, l, c, i;
int a[] = {1, 153, 370, 371, 407};
while (scanf("%d%d", &b, &l) != EOF)
{
c = 0;
for (i = 0 ; i < 5 ; i++)
{
if (a[i] >= b && a[i] <= l)
printf(c++ ? " %d" : "%d", a[i]);
}
printf(c ? "\n" : "no\n");
}
return 0;
}
第二种解法:
int sxh(int m)
{
int sum = 0;
int n = m;
while (n)
{
sum += (n % 10)*(n % 10)*(n % 10);
n /= 10;
}
return sum == m;
}
主函数直接调用就可以了。
int main(void)
{
int n, m, c;
while (scanf("%d%d", &m, &n))
{
for (c = 0; m <= n; m++)
{
if (sxh(m))
printf(c++ ? " %d": "%d", m);
}
puts(c ? "\n" : "no\n");
}
return 0;
}