当前位置:首页 > JAVA教程 >

打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数

发布时间:2015-11-21 07:50:37 作者:佚名 阅读:(90)

import java.util.Scanner;
public class text4{
    public static void main(String[]args){
        int x;  //x个数相加;
        System.out.println("请输入加数项的个数:");
        Scanner in = new Scanner(System.in);
        x = in.nextInt();
        int a;  //每位的值a;
        System.out.println("请输入加数每位的值:");
        a = in.nextInt();    
        int c[] = new int[x];   //第i个加数的值;
        int b[] = new int[x];   //第i位的值; 
        int i,j;
        int s = 0;
        for(i = 0;i < x;i++){
            int tempb = 0;
            tempb = (int)Math.pow(10, i);
            b[i] = a * tempb;   
            for(j = 0;j < x;j++){
                c[j] += b[i];
            }
            if(i==(x-1)){
                System.out.print(c[i]+"=");
            }else{
                System.out.print(c[i]+"+");
            } 
            s += c[i];
        }
        System.out.print(s);
    }
}      //版权所有    吾爱IT编程www.itbiancheng.com

欢迎分享转载→ 打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数

© 2015-2021 - 吾爱编程网 版权所有 苏ICP备18033726号-1关于我们 - 网站声明 - 联系我们