阿姆斯壮数
在三位的整數(shù)中,例如153可以滿足13?+ 53?+ 33?= 153,這樣的數(shù)稱之為Armstrong數(shù),試寫出一程式找出所有的三位數(shù)Armstrong數(shù)。
解法
?Armstrong數(shù)的尋找,其實(shí)就是在問如何將一個(gè)數(shù)字分解為個(gè)位數(shù)、十位數(shù)、百位數(shù)......,這只要使用除法與余數(shù)運(yùn)算就可以了,例如輸入 input為abc,則:
a = input / 100
b = (input%100) / 10
c = input % 10
#include <stdio.h> #include <time.h> #include <math.h> int main(void) { int a, b, c; int input; printf("尋找Armstrong數(shù):\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 ", input); } printf("\n"); return 0; }?
總結(jié)