水仙花數:三位數 abc,滿足 a^3 + b^3 + c^3 = abc。遍歷 100-999 判斷並輸出。
// Created by www.runoob.com on 15/11/9.
// Copyright © 2015年 菜鳥教程. All rights reserved.
//
#include <stdio.h>
int main() {
int i, x, y, z;
for (i = 100; i < 1000; i++) {
x = i % 10;
y = i / 10 % 10;
z = i / 100 % 10;
if (i == (x*x*x + y*y*y + z*z*z))
printf("%d\n", i);
}
return 0;
}
153
370
371
407
可推廣至 n 位數(阿姆斯壯數),冪次為位數 n。