水仙花数

来源:互联网 发布:nginx php exec 编辑:程序博客网 时间:2024/05/13 05:12

水仙花数又称阿姆斯特朗数。

水仙花数是指一个n 位数( n3 ),它的每个位上的数字的n 次幂之和等于它本身。(例如:1^3 + 5^3 + 3^3 = 153)

求输入的数字是否为水仙花数


#include <stdlib.h>#include "oj.h"#include <vector>#include <math.h>using namespace std;// 功能:判断输入 nValue 是否为水仙花数// 输入: nValue为正整数// 输出:无// 返回:如果输入为水仙花数,返回1,否则返回0unsigned int IsDaffodilNum(unsigned int  nValue){int bitNum = 0;unsigned int value = nValue;vector<int> bit;while(value){bit.push_back(value%10);value /= 10;++bitNum;}if(bitNum < 3)return 0;value = 0;for(vector<int>::iterator it = bit.begin();it != bit.end();++it){value += static_cast<int>(pow(static_cast<float>(*it),bitNum));}if(value == nValue)return 1;elsereturn 0;}


0 0
原创粉丝点击