程序员 2008-5-8 14:02
水仙花数问题探求
/*
* 题目:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,
* 其各位数字立方和等于该数本身。
* 例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。
* 思路分析:
* 1、首先该数是一个3位数,即100-999
* 2、解析出各位数字,各位数立方和等于该数
*
*/
#include <iostream>
using namespace std;
int main(void)
{
int n;
int i, j, k;
for(n=100; n<1000; n++)
{
i = n/100;
j = (n - i*100)/10;
k = n%10;
if( i*i*i + j*j*j + k*k*k == n )
{
cout << n << endl;
}
}
getchar();
return 0;
}
程序员 2008-5-8 14:04
另一个
#include <iostream>
using namespace std;
int main()
{
int hundreds,tens,ones;
int result=hundreds*100+tens*10+ones;
for(hundreds=0;hundreds<=9;hundreds++)
for(tens=0;tens<=9;tens++)
for(ones=0;ones<=9;ones++)
if(result==hundreds*hundreds*hundreds+tens*tens*tens+ones*ones*ones)
cout<<result<<endl;
return 0;
}
liumingxing 2008-5-16 21:48
是高了
程序讲的就是效率
liumingxing 2008-5-16 21:59
现在觉得是低了,呵呵
xpweixiang 2008-5-24 22:09
感觉第二个低点,多计算了0-99这100个数