Java的10道经典例题

【程序1】 题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。

public static void main(String[] args) {
        int i = 0;
        int j = 0;
        int k = 0;
        int t = 0;
        for(i = 1;i<=4;i++) {
            for(j = 1;j<= 4;j++) {
                for (k = 1;k<=4;k++) {
                    if(i != j && j != k && i != k) {
                        t+=1;
                        System.out.println(i*100+j*10+k);
                    }
                }
            }
        }

    }

运行结果:

123
124
132
134
142
143
213
214
231
234
241
243
312
314
321
324
341
342
412
413
421
423
431
432

【程序2】 题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?
程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。

public static void main(String[] args) {
        double sum;//声明要储存的变量应发的奖金
        Scanner input =new Scanner (System.in);//导入扫描器
        System.out.print ("输入当月利润");
        double lirun=input .nextDouble();//从控制台录入利润
        if(lirun<=100000){
        sum=lirun*0.1;
        }else if(lirun<=200000){
        sum=10000+lirun*0.075;
        }else if(lirun<=400000){
        sum=17500+lirun*0.05;
        }else if(lirun<=600000){
        sum=lirun*0.03;
        }else if (lirun<=1000000){
        sum=lirun*0.015;
        } else{
        sum=lirun*0.01;
        }
        System.out.println("应发的奖金是"+sum);
}

运行结果:

输入当月利润5800
应发的奖金是580.0

【程序3】题目:一个整数,它加上100后是一个完全平方数,加上168又是一个完全平方数,请问该数是多少?
程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后的结果满足如下条件,即是结果。请看具体分析:

public static void main(String[]args){
        long k=0;
        for(k=1;k<=100000l;k++)
        if(Math.floor(Math.sqrt(k+100))==Math.sqrt(k+100) && Math.floor(Math.sqrt(k+168))==Math.sqrt(k+168))
        System.out.println(k);

        }

运行结果:

156

【程序4】 题目:输入某年某月某日,判断这一天是这一年的第几天?
程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。

public static void main(String[]args){
        int day=0;
        int month=0;
        int year=0;
        int sum=0;
        int leap;
        System.out.print("请输入年,月,日\n");
        Scanner input = new Scanner(System.in);
        year=input.nextInt();
        month=input.nextInt();
        day=input.nextInt();
        switch(month) {/*先计算某月以前月份的总天数*/
        case 1:
            sum=0;
            break;
        case 2:
            sum=31;
            break;
        case 3:
            sum=59;
            break;
        case 4:
            sum=90;
            break;
        case 5:
            sum=120;
            break;
        case 6:
            sum=151;
            break;
        case 7:
            sum=181;
            break;
        case 8:
            sum=212;
            break;
        case 9:
            sum=243;
            break;
        case 10:
            sum=273;
            break;
        case 11:
            sum=304;
            break;
        case 12:
            sum=334;
            break;
        default:
            System.out.println("data error");break;
        }
        sum=sum+day; /*再加上某天的天数*/
        if(year%400==0||(year%4==0&&year%100!=0)) {/*判断是不是闰年*/
                  leap=1;
        }
        else {
            leap=0;
        }
        if(leap==1 && month>2) {/*如果是闰年且月份大于2,总天数应该加一天*/
            sum++;
        }
        System.out.println("它是这一年的第:"+sum+"天");

        }

运行结果:

请输入年,月,日
2018
7
4
它是这一年的第:185

【程序5】 题目:输入三个整数x,y,z,请把这三个数由小到大输出。
程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x> y则将x与y的值进行交换,然后再用x与z进行比较,如果x> z则将x与z的值进行交换,这样能使x最小。

public static void main(String[]args){
        int i=0;
        int j=0;
        int k=0;
        int x=0;
        System.out.print("请输入三个数\n");
        Scanner input = new Scanner(System.in);
        i=input.nextInt();
        j=input.nextInt();
        k=input.nextInt();
        if(i>j){
            x=i;
            i=j;
            j=x;
            }

        if(i>k){
            x=i;
            i=k;
            k=x;
            }

        if(j>k){
            x=j;
            j=k;
            k=x;
            }
        System.out.println(i+", "+j+", "+k);

        }
请输入三个数
5
8
4
4, 5, 8

【程序6】 题目:输出9*9口诀。
程序分析:分行与列考虑,共9行9列,i控制行,j控制列。

package study;
//输出九九乘法表
public class JiuJiuBiao {
    public static void sum(int n){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=i;j++){
                System.out.print(j+"*"+i+"="+i*j+"  ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
          sum(9);
    }

}
1*1=1  
1*2=2  2*2=4  
1*3=3  2*3=6  3*3=9  
1*4=4  2*4=8  3*4=12  4*4=16  
1*5=5  2*5=10  3*5=15  4*5=20  5*5=25  
1*6=6  2*6=12  3*6=18  4*6=24  5*6=30  6*6=36  
1*7=7  2*7=14  3*7=21  4*7=28  5*7=35  6*7=42  7*7=49  
1*8=8  2*8=16  3*8=24  4*8=32  5*8=40  6*8=48  7*8=56  8*8=64  
1*9=9  2*9=18  3*9=27  4*9=36  5*9=45  6*9=54  7*9=63  8*9=72  9*9=81  

【程序7】 题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下 的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
.程序分析:采取逆向思维的方法,从后往前推断。

public class LianXi {
    static int total(int day){
        if(day == 10){
            return 1;
            }
        else{
            return(total(day+1)+1)*2;
            }
        }
    public static void main(String[]args){
        System.out.println(total(1));
        }

}

运行结果:

1534

【程序8】 题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。

package study;

import java.util.ArrayList;

public class pingpang {
    String a,b,c;
    public static void main(String[] args) {
        String[] op = { "x", "y", "z" };
        ArrayList<pingpang> arrayList=new ArrayList<pingpang>();
        for(int i = 0; i < 3; i++) { 
            for(int j = 0; j < 3; j++) {
                for(int k = 0; k < 3; k++) {
                    pingpang a=new pingpang(op[i],op[j],op[k]);
                    if(!a.a.equals(a.b)&&!a.b.equals(a.c)&&!a.a.equals("x")&&!a.c.equals("x")&&!a.c.equals("z")){
                        arrayList.add(a);
                        }
                }
            }
        }

        for(Object a:arrayList){
            System.out.println(a);
            }
        }
    public pingpang(String a, String b, String c) {
        super();
        this.a = a;
        this.b = b;
        this.c = c;
        }
    public String toString() {
        return "a的对手是"+a+","+"b的对手是"+b+","+"c的对手是"+c+"\n";
        }

}

运行结果:

a的对手是y,b的对手是x,c的对手是y

a的对手是y,b的对手是z,c的对手是y

a的对手是z,b的对手是x,c的对手是y

【程序9】 题目:打印出如下图案(菱形)
这里写图片描述
程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重 for循环,第一层控制行,第二层控制列。

//三角形
public class LianXi {
    public static void main(String[] args) {
        int i=0;
        int j=0;
        for(i=1;i<=4;i++){
            for(j=1;j<=2*i-1;j++) {
                System.out.print("*");
                }
            System.out.println("");
            }

        for(i=4;i>=1;i--){
            for(j=1;j<=2*i-3;j++){
                System.out.print("*");
            }
            System.out.println("");
            }
        }
}
*
***
*****
*******
*****
***
*
//菱形
public class LianXi {
    public static void main(String[] args) {
        int i=0;
        int j=0;
        for(i=1;i<=4;i++){
            for(int k=1; k<=4-i;k++){
                System.out.print(" ");
            }
            for(j=1;j<=2*i-1;j++){
                System.out.print("*");
            }
            System.out.println("");
            }
        for(i=4;i>=1;i--){
            for(int k=1; k<=5-i;k++){
                System.out.print(" ");
            }
            for(j=1;j<=2*i-3;j++){
                System.out.print("*");
            }
            System.out.println("");
            }
        }
    }

   *
  ***
 *****
*******
 *****
  ***
   *
package javayouquqenti;
//打印一颗小树图像
public class XioaShu {
    public static void main(String[] args) {
        for(int i=1;i<=9;i++){
            if(i<=5){//打印出一个三角形,5层,作为小树的枝叶部分
                for(int k=i;k<=4;k++){
                    System.out.print(" ");//第i行对应4-i个空格
                }
                for(int j=1;j<=2*i-1;j++){
                    System.out.print("*");//第一行打印2*i-1个*
                }
            }
            else{//5行以后是小树的主干部分
                for(int k=1;k<=3;k++){
                    System.out.print(" ");
                }
                for(int j=1;j<=3;j++){
                    System.out.print("*");
                }
            }
            System.out.println();
        }
    }

}
    *
   ***
  *****
 *******
*********
   ***
   ***
   ***
   ***

【程序10】 题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13…求出这个数列的前20项之和。
.程序分析:请抓住分子与分母的变化规律。

public class LianXi {
    public static void main(String[] args) {
        float fm = 1f;
        float fz = 1f;
        float temp;
        float sum = 0f;
        for(int i=0;i<20;i++){
            temp = fm;
            fm = fz;
            fz = fz + temp;
            sum += fz/fm;
            }
        System.out.println(sum);
        }

 }
32.660263

猜你喜欢

转载自blog.csdn.net/qq2899349953/article/details/80906570
今日推荐