计算水仙花数

[入库:2005年8月19日] [更新:2007年3月24日]

本文简介:选择自 auding1900 的 blog

/*
 有道题是输出所有的水仙花数,水仙花数是指一个3位数,
 其各位数字的立方和等于该数本身。例如:153=1的3次方+5的3次方+3的3次方。
*/

#include <iostream>
#include <conio.h>

using namespace std;

#define trimp(x) (x)*(x)*(x)

int main()
{
 cout<<"水仙花数有:"<<endl;

 int b, c, d, tmp;
 b = c = d = 0;
 for(int n = 100; n<=999; ++n)
 {
  b = trimp(n/100);
  c = trimp((n%100)/10);
  d = trimp((n%100)%10);
  tmp = b + c + d;
  if(n == tmp)
   cout << n << endl;
 }

 getch();
 return 0;
}

本文关键:计算水仙花数
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top