水仙花數

來源:互聯網
上載者:User

標籤:華為上機題

水仙花數又稱阿姆斯特朗數。

水仙花數是指一個n 位元( n≥3 ),它的每個位上的數位n 次冪之和等於它本身。(例如:1^3 + 5^3 + 3^3 = 153)

求輸入的數字是否為水仙花數

此題糾正了我一個錯誤的認識,我一直以為水仙花數是每位的立方和等於這個數,原因是以前經常求的是三位元.

完整滿分代碼如下:

#include "oj.h"// 功能:判斷輸入 nValue 是否為水仙花數// 輸入: nValue為正整數// 輸出:無// 返回:如果輸入為水仙花數,返回1,否則返回0unsigned 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;}


 

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

水仙花數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.