码迷,mamicode.com
首页 > 其他好文 > 详细

水仙花数

时间:2015-07-03 23:28:21      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:华为上机题

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

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

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

此题纠正了我一个错误的认识,我一直以为水仙花数是每位的立方和等于这个数,原因是以前经常求的是三位数.

完整满分代码如下:

#include "oj.h"

// 功能:判断输入 nValue 是否为水仙花数
// 输入: nValue为正整数
// 输出:无
// 返回:如果输入为水仙花数,返回1,否则返回0
unsigned int IsDaffodilNum(unsigned int  nValue)
{
	if(nValue<100)
		return 0;
	
	long  n=nValue;
	long  sum=0;
	int i;
	int cnt=0;
	int tmp=1;
	while(nValue)
	{
		nValue/=10;
		cnt++;
	}

	nValue=n;

	while(nValue)
	{
		tmp=1;
		i=nValue%10;
		for(int j=0;j<cnt;j++)
		{
			tmp*=i;
		}
		sum+=tmp;
		nValue/=10;
	}
	if(sum==n)
		return 1;
    else
    	return 0;
}


 

版权声明:本文为博主原创文章,未经博主允许不得转载。

水仙花数

标签:华为上机题

原文地址:http://blog.csdn.net/persever/article/details/46746787

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!