水仙花数
水仙花数指一个三位数,它的每个位上的数字的3次幂之和等于它本身
例如:1^3+5^3 +3^3 =153
利用do...while语句求出所以的3位数的水仙花数
#include<iostream>
using namespace std;
int main() {
int num = 100;
do {
int a = 0; //个位数
int b = 0; //十位数
int c = 0; //百位数
a = num % 10;
b = num / 10 % 10;
c = num / 100;
if (a*a*a + b * b*b + c * c*c == num) {
cout << num << endl;
}
num++;
} while (num<1000);
system("pause");
return 0;
}
评论 (0)