C++编程入门--求水仙花数

mac2022-06-30  97

C++编程入门--求水仙花数

【C++程序设计教程第二版–钱能】编程求所有的“水仙花数”(narcissus number)。所谓的“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。例如,153是水仙花数,因为153=1 3+5 3+3 3

#include<iostream> using namespace std; int main() { cout << "三位水仙花数有:"; for (int a = 100; a <= 999; a++) { if (a == (a % 10)*(a % 10)*(a % 10) + (a / 100)*(a / 100)*(a / 100) + (a % 100 / 10)*(a % 100 / 10)*(a % 100 / 10)) cout << a << endl; } system("pause"); return 0; }

最新回复(0)