zoukankan      html  css  js  c++  java
  • java求1000以内的水仙花数

    水仙花数是指一个 n 位数 ( n>=3 ),它的每个位上的数字的 n 次幂之和等于它本身。(例如:1^3 + 5^3 + 3^3 = 153)

      三位的水仙花数共有4个,分别为:153、370、371、407

    代码实现:

    public class For_Demo2 {
    		public static void main(String[] args) {
    			//求水仙花数
    			int ge,shi,bai;
    			int m=0;
    			int total=0;
    			for(int i=100;i<1000;i++){
    				ge=i%10;
    				shi=i/10%10;
    				bai=i/100;
    				int b=(int) (Math.pow(ge,3)+Math.pow(shi, 3)+Math.pow(bai, 3));
    				if(i==b){	
    					System.out.println(i);
    					total+=i;
    					m++;	
    				}
    			}
    			System.out.println("total:"+total);
    			System.out.println(m);
    		}
    }

    注意:取出每位的值。
     

  • 相关阅读:
    JAVA 练习1
    JSP基础
    网络协议
    mysql基础
    python之高级
    powershell基础
    python之迭代器与遍历
    python之面向对象
    linux常用命令
    docker 安装 ElasticSearch:7.4.2
  • 原文地址:https://www.cnblogs.com/jatpeo/p/11767576.html
Copyright © 2011-2022 走看看