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


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

    输入格式:

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

    输出格式:

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

    输入样例:
    3
    
    输出样例:
    153
    370
    371

    此题借用他人的算法,十分简短。

     

    #include <iostream>
    #include <stdio.h>
    #include <math.h>
    
    
    
    #include<string>
    
    int main(){
    
        int n;
        scanf("%d",&n);
       //计算出每个个位数的N次方
       int p[10];
       for(int i=0;i<10;i++)
       {
       	p[i]=pow(i,n);
       	//printf("%d
    ",p[i]);
       } 
       //遍历N位数
       int num; 
       for(num=pow(10,n-1);num<pow(10,n);num++)
       {
       	//相除得到每位数
    	   int j;//每个位数,存储最低位
    	   int temp=num;
    	   int sum=0;
    	   while(temp!=0)
    	   {
    	   	sum+=p[temp%10];
    	   	temp=temp/10;
    	   } 
    	   if(sum==num)
    	   {
    	   	printf("%d
    ",sum);
    	   }
       }
        return 0;
    }
    

      

     

    
    
  • 相关阅读:
    Python循环语句
    Python简单的语句组
    Jedis 之 初始<一>
    微信小程序登入流程
    微信小程序发起请求
    django数据库迁移时候异常
    Git常用命令总结
    微信小程序自定义组件
    POJ3345 Bribing FIPA
    POJ1947 Rebuilding Roads
  • 原文地址:https://www.cnblogs.com/ligen/p/4246138.html
Copyright © 2020-2023  润新知