java简单编程(求和,求水仙花数,求完备数)

1./*功能:编程试题:求数列的和

数列的定义如下: 数列的第一项为n,以后各项为前一项的平方根,求数列的前m项的和。

输入数据有多组,每组占一行,由两个整数n(n<10000)和m(m<1000)组成,n和m的含义如前所述。

对于每组输入数据,输出该数列的和,每个测试实例占一行,要求精度保留2位小数。

 */

packagecom.zyy.code1;

importjava.text.DecimalFormat;

importjava.util.Scanner;

publicclass GetSum1 {

       public static void main(String args[]){

        int m;

        double sum,n;

        Scanner sc = new Scanner(System.in);

        while(sc.hasNext()){

            n=sc.nextInt();

            m=sc.nextInt();

            sum=0;

            for(int i=0;i

                sum=sum+n;

                n=Math.sqrt(n);

            }

           DecimalFormat df=newDecimalFormat("#.00");

           System.out.print(df.format(sum));

            System.out.println();

        }

    }

}

2.求水仙花数

方法1:

/*

 *  * 题目:打印出m-n之间所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。

 *   例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。

 */

packagecom.zyy.code1;

importjava.util.Scanner;

publicclass SuiXianHua {

       public static void main(String[] args){

              int m, n;

              Scanner sc=new Scanner(System.in);

              while(sc.hasNext()){

                     m=sc.nextInt();

                     n=sc.nextInt();

                     int bai, shi, ge;

                     if(m>99&&m

                     for(int i=100;i

                            bai=i/100;

                            shi=i%100/10;

                            ge=i%100%10;

                            if(i==bai*bai*bai+shi*shi*shi+ge*ge*ge){

                                   System.out.print(i+"");

                            }

                     }else{System.out.print("输入的数不满足要求!");}

                     System.out.println();

              }

       }

}

方法2:求水仙花数

packagecom.zyy.code1;

importjava.util.Scanner;

publicclass ShuiXianHua1 {

       public static void main(String[] args) {

              int m, n;

              Scanner sc=new Scanner(System.in);

              while(sc.hasNext()){

                     m=sc.nextInt();

                     n=sc.nextInt();

                     int bai, shi, ge;

                     if(m>99&&m

                            for(inti=m;i

                                   bai=i/100;

                                   shi=(i-bai*100)/10;

                                   ge=i-bai*100-shi*10;

                                   if(i==bai*bai*bai+shi*shi*shi+ge*ge*ge){

                                          System.out.print(i+"");

                                   }

                            }else{System.out.print("输入的数不满足要求!");}

                     System.out.println();

              }

       }

}

3.求完备数

/**

 * 完全数:(PerfectNumber)又称完美数或完备数,是一些特殊的自然数。它所有的真因子(即除了自身以外的约数)的和(即因子函数),

 * 恰好等于它本身。如果一个数恰好等于它的因子之和,则称该数为“完全数”。

需求:判断并输出n以内的所有完全数。

题目:一个数如果恰好等于它的因子之和,这个数就称为"完数 "。例如6=1+2+3.编程 找出1000以内的所有完数。

 */

packagecom.zyy.code1;

 

importjava.util.Scanner;

 

publicclass WanBeiShu {

       public static void main(String[] args){

              Scanner sc=new Scanner(System.in);

              while(sc.hasNext()){

                     int n=sc.nextInt();

             

                     for(int i=1;i

                            int sum=0;

                            for(intj=1;j

                                   if(i%j==0){

                                          sum+=j;

                                   }

                            }

                            if(sum==i){

                                   System.out.print(i+"");

                            }

                     }

                     System.out.println();

              }

       }

 

}

你可能感兴趣的:(典型编程(java))