[Huawei Machine Test exercises]48. Armstrong number

Source: Internet
Author: User

Topic

Describe:

如果一个正整数等于其各个数字的立方和,则该数称为阿姆斯特朗数(亦称为自恋性数),1除外,如407 = 43+03+73就是一个阿姆斯特朗数。试编程求n(n ≤ 65536)以内的所有阿姆斯特朗数。

Interface description
Prototype:

int CalcArmstrongNumber(int n);

Input parameters:

int n: n ≤ 65536

return value:

   n以内的阿姆斯特朗数的数量

Practice Stage:

Code

/* ---------------------------------------* Date: 2015-07-04* sjf0115* title: Armstrong number * Source: Huawei Machine Test Exercises--------------------- --------------------*/#include <iostream>#include "OJ.h"using namespace Std;/ * Function: For all Armstrong number prototypes within N (n≤65536): int calcarmstrongnumber (int n); input parameter: int n:n≤65536 return value: Number of Armstrong within N. */intCalcarmstrongnumber (intN) {if(N <=1){return 0; }//if    int sum, M,tmp,Count=0; for(intI=2; I <= n;++i) {sum=0; TMP = i; while(TMP) {m = tmp%Ten;sum+ = m * m * m; TMP/=Ten; }//while        if(sum= = i) {+ +Count; }//if}//if    return Count;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[Huawei Machine Test exercises]48. Armstrong number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.