【阿姆斯壮数 】

/*
阿姆斯壮数 

说明:
在三位的整数中,例如153可以满足1^3 + 5^3 + 3^3 = 153,这样的数称之为Armstrong数,试写出一程式找出所有的三位数Armstrong数。

解法:
Armstrong数的寻找,其实就是在问如何将一个数字分解为个位数、十位数、百位数......,这只要使用除法与余数运算就可以了,例如输入 input
为abc,则:
a = input / 100
b = (input%100) / 10
c = input % 10

*/ 

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void)
{
    int a, b, c;
    int input;
    
    printf("search Armstrong numbers in 999: \n");
    
    for(input = 100; input <= 999; input++)
    {
        a = input / 100;
        b = (input % 100) / 10;
        c = input % 10;
        if(a * a * a + b * b * b + c * c * c == input)
        {
            printf("%d \n", input);
        }
    }
    
    printf("\n");
    
    return 0;
}

 

结果:

 

posted @ 2017-01-20 10:48  天秤libra  阅读(1608)  评论(0编辑  收藏  举报