vc++编程求解,
设计一个程序,输出所有的水仙花数。水仙花数是个三位数,其各位数字立方和等于该数本身。例如;153=1的立方+5的立方+3的立方
设计一个程序,输出所有的水仙花数。水仙花数是个三位数,其各位数字立方和等于该数本身。例如;153=1的立方+5的立方+3的立方
答案:运行截图:
程序:
#include <iostream>
#include <cmath>
using namespace std;int main(void){
for (int i = 100; i != 1000; ++i)
if (pow((i % 10), 3.0) +
pow((i / 100), 3.0) +
pow((i / 10 % 10), 3.0) == i)
cout << i << endl;
system("PAUSE>NUL");
return 0;
}#include<stdio.h>
main()
{ int i,j,k,n;
printf("“水仙花”数是:");
for(n=100;n<1000;n++)
{ i=n/100;
j=n/10-i*10;
k=n%10;
if(n==i*i*i+j*j*j+k*k*k)
printf("%4d",n);
}
printf("\n");
}