PAT循环-11. 水仙花数(20)

来源:互联网 发布:mac sass安装失败 编辑:程序博客网 时间:2024/05/20 01:34

循环-11. 水仙花数(20)

时间限制
2000 ms
内存限制
32000 kB
代码长度限制
8000 B
判题程序
Standard
作者
徐镜春(浙江大学)

水仙花数是指一个N位正整数(N>=3),它的每个位上的数字的N次幂之和等于它本身。例 如:153 = 13 + 53+ 33。 本题要求编写程序,计算所有N位水仙花数。

输入格式:

输入在一行中给出一个正整数N(3<=N<=7)。

输出格式:

按递增顺序输出所有N位水仙花数,每个数字占一行。

输入样例:
3
输出样例:
153370371407
#include<stdio.h>int getPowSum(int n,int pow);int main(){int N;scanf("%d",&N);int from=1,to=1,i;for(i=1;i<N;i++)from=from*10;to=from*10-1;//printf("%d to %d\n",from,to);for(i=from;i<to;i++)if(getPowSum(i,N)==i)printf("%d\n",i);}int getPowSum(int n,int pow){int sum=0;int shang=n/10;int yushu=n%10;int i,temp;while(shang!=0 || yushu!=0){temp=1;for(i=0;i<pow;i++)temp=temp*yushu;sum=sum+temp;yushu=shang%10;shang=shang/10;}return sum;}


0 0
原创粉丝点击