阿姆斯壮数
在三位的整數中,例如153可以滿足13?+ 53?+ 33?= 153,這樣的數稱之為Armstrong數,試寫出一程式找出所有的三位數Armstrong數。
解法
?Armstrong數的尋找,其實就是在問如何將一個數字分解為個位數、十位數、百位數......,這只要使用除法與余數運算就可以了,例如輸入 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數:\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; }?
總結