[华为机试练习题]60.水仙花数

简介:

题目

描述:

水仙花数又称阿姆斯特朗数。
水仙花数是指一个n 位数( n≥3 ),它的每个位上的数字的n 次幂之和等于它本身。(例如:1^3 + 5^3 + 3^3 = 153)
求输入的数字是否为水仙花数

练习阶段:

初级 

代码

/*---------------------------------------
*   日期:2015-07-05
*   作者:SJF0115
*   题目:水仙花数
*   来源:华为机试练习题
-----------------------------------------*/
#include <iostream>
#include "oj.h"
#include <vector>
using namespace std;

// 分解
vector<int> NumSplit(int num){
    vector<int> result;
    if(num == 0){
        result.push_back(0);
        return result;
    }//if
    if(num < 0){
        return result;
    }//if
    int tmp = num;
    while(tmp){
        result.insert(result.begin(),tmp % 10);
        tmp /= 10;
    }//while
    return result;
}
// N次幂
unsigned int NOfNum(int num,int size){
    if(size <= 0){
        return 0;
    }//if
    unsigned int result = 1;
    for(int i = 0;i < size;++i){
        result *= num;
    }//for
    return result;
}

// 功能:判断输入 nValue 是否为水仙花数
// 输入: nValue为正整数
// 输出:无
// 返回:如果输入为水仙花数,返回1,否则返回0
unsigned int IsDaffodilNum(unsigned int  nValue){
    if(nValue <= 0){
        return 0;
    }//if
    vector<int> numvec = NumSplit(nValue);
    int size = numvec.size();
    int result = 0;
    for(int i = 0;i < size;++i){
        result += NOfNum(numvec[i],size);
        if(result > nValue){
            return 0;
        }//if
    }//for
    if(result == nValue){
        return 1;
    }//if
    return 0;
}

目录
相关文章
|
8月前
【刷题日志】深度理解除(/)与取模(%)附水仙花数以及变种水仙花数题解
【刷题日志】深度理解除(/)与取模(%)附水仙花数以及变种水仙花数题解
每日一题——“水仙花数”
哈喽大家好,我是保护小周ღ,本期为大家带来的是求“水仙花数”,此水仙花,非彼水仙花,一起来看看把~
133 0
|
C++
蓝桥杯练习题十 - 煤球数目(c++)
蓝桥杯练习题十 - 煤球数目(c++)
154 0
蓝桥杯练习题十 - 煤球数目(c++)
|
C++
蓝桥杯练习题四 - 排它平方数(c++)
蓝桥杯练习题四 - 排它平方数(c++)
115 0
python:考试前,练手习题(斐波那契数,字符串排序,九九乘法表,水仙花数,求和...求数字倍数,(保佑不挂科!)
整理一些练手的题目(含代码),可能不是特别优美,有些繁琐,但是等有时间再优化吧~~ 1.水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153)。 请编写程序,在一行内,按从小到大的顺序输出所有水仙花数,以空格作为分割
|
算法
LeetCode每日一题——668. 乘法表中第k小的数
几乎每一个人都用 乘法表。但是你能在乘法表中快速找到第k小的数字吗?
122 0
牛客网——变种水仙花数
牛客网——变种水仙花数
132 0
|
C语言
浙大版《C语言程序设计(第3版)》题目集习题5-6 使用函数输出水仙花数 (20 分)
水仙花数是指一个N位正整数(N≥3),它的每个位上的数字的N次幂之和等于它本身。例如:153=1 ​3 ​​ +
539 0
浙大版《C语言程序设计(第3版)》题目集习题5-6 使用函数输出水仙花数 (20 分)
|
C语言
浙大版《C语言程序设计(第3版)》题目集 - 习题4-6 水仙花数(20 分)
浙大版《C语言程序设计(第3版)》题目集 - 习题4-6 水仙花数(20 分)
89 0
力扣每日一题:633.平方数之和
力扣每日一题:633.平方数之和
109 0