水仙花数指的是一个三位正整数,它等于每个位上的数字的 3次幂之和,比如153=1^3+5^3+3^3,编程打印出所有的水仙花数。(循环作业)

#include<iostream>
#include<cmath>
using namespace std;
int main() {
int x = 100;
do {
int a = 0;
int b = 0;
int c = 0;
a = x % 10;
b = x / 10 % 10;
c = x / 100 % 10;
if (pow(a, 3) + pow(b, 3) + pow(c, 3) == x) {
cout << x << endl;
}
x++;
} while (x < 1000);
return 0;
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzjgpp.html