#include <stdio.h>
int main(int argc, const char *argv[])
{
int n;
int a,b,c;
n = 100;
while(n <= 999){
a = n / 100;
b = n % 100 / 10;
c = n % 100 % 10;
if (n == a*a*a + b*b*b + c*c*c){
printf("%d\n",n);
}
n++;
}
return 0;
}
运行结果:
2.使用do while构造循环
#include <stdio.h>
int main(int argc, const char *argv[])
{
int num = 100;
int a,b,c;
do{
a = num /100;
b = num % 100 /10;
c = num %100 %10;
if((a*a*a + b*b*b +c*c*c) == (a * 100 +b * 10 +c)){
printf("%d\n",num);
}
num++;
}while(num <= 999);
return 0;
}