大家好:
衷心希望各位点赞。
您的问题请留在评论区,我会及时回答。
案例描述
水仙花数,是指一个3位数,它的每个位上的数字的3次幂之和等于它本身。例如:1^3 + 5^3 + 3^3 = 153,利用 do……while 语句,求出3位数中的所有水仙花数。
代码
#include <iostream>
#include <Windows.h>
using namespace std;
//输出水仙花数
int main(void) {
int num = 100;
cout << "三位数中所有的水仙花数为:" << endl;
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 <= 999);
system("pause");
return 0;
}
运行截图: