/*求水仙花數。輸入一個正整數n,計算n位水仙花數。*/
#include<stdio.h> //編譯預處理命令
int mypow (int x, int n); //聲明自己的冪函數
int main (int agrc, char const *agrv[]) //主函數
{
int n, start, end, temp, sum; //變量定義
scanf("%d", &n); //輸入正整數
start = mypow(10, n-1); //計算最小的n位正整數
end = start*10 - 1; //計算最大的n位正整數
for ( ; start < end; start++) //遍歷所有的n位正整數
{
temp = start;
sum = 0;
while(temp)
{
sum += mypow(temp%10, n); //計算其各位數的n次方之和
temp /= 10;
}
if (sum == start)
printf("%d\n", start); //如果滿足各位數的n次方之和等於其本身,就輸出該水仙花數
}
return 0;
}
int mypow (int x, int n) //定義自己的冪函數
{
int ret;
ret = 1; //ret的初始值必須設爲1否則會出錯
while(n--) //當n等於0時,循環停止
ret *= x;
return ret;
}
#運行程序,輸入一個正整數n,計算n位水仙花數。