您的当前位置:首页>全部文章>文章详情

Java经典算法40题(二)

发表于:2024-02-24 22:22:40浏览:141次TAG: #Java知识

【程序11】 1234个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?  

程序分析:可填在百位、十位、个位的数字都是1234。组成所有的排列后再去 掉不满足条件的排列。  

 

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
public class Wanshu {    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);                    }        System.out.println (t);    }}

 

【程序12】 企业发放的奖金根据利润提成。利润(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,求应发放奖金总数?  

程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。

 

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
import java .util.*;public class test {    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);    }}

 

【程序13一个整数,它加上100后是一个完全平方数,加上168又是一个完全平方数,请问该数是多少?

程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后的结果满足如下条件,即是结果。请看具体分析:

 

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
public class test {    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);    }}

 

【程序14】 输入某年某月某日,判断这一天是这一年的第几天?

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

 

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
import java.util.*;public class test {    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("It is the the day:"+sum);    }}

 

【程序15】输入三个整数x,y,z,请把这三个数由小到大输出。

程序分析:我们想办法把最小的数放到x上,先将xy进行比较,如果x> y则将xy的值进行交换,然后再用xz进行比较,如果x> z则将xz的值进行交换,这样能使x最小。

 

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
import java.util.*;public class test {    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);    }}

 

【程序16】输出9*9口诀。

程序分析:分行与列考虑,共99列,i控制行,j控制列。

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
public class jiujiu {    public static void main(String[] args)    {        int i=0;        int j=0;        for(i=1;i<=9;i++)        {  for(j=1;j<=9;j++)            System.out.print(i+"*"+j+"="+i*j+"\t");            System.out.println();        }    }}

不出现重复的乘积(下三角)

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
public class jiujiu {    public static void main(String[] args)    {        int i=0;        int j=0;        for(i=1;i<=9;i++)        {  for(j=1;j<=i;j++)            System.out.print(i+"*"+j+"="+i*j+"\t");            System.out.println();        }    }}

上三角

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
public class jiujiu {    public static void main(String[] args)    {        int i=0;        int j=0;        for(i=1;i<=9;i++)        {  for(j=i;j<=9;j++)            System.out.print(i+"*"+j+"="+i*j+"\t");            System.out.println();        }    }}

 

【程序17猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个   第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下   的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。  

程序分析:采取逆向思维的方法,从后往前推断。

 

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
public class MokeyEat {    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));    }}

 

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

程序分析:判断素数的方法:用一个数分别去除2sqrt(这个数),如果能被整除, 则表明此数不是素数,反之是素数。  

 

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
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;    }    @Override    public String toString()    {        // TODO Auto-generated method stub        return "a的对手是"+a+","+"b的对手是"+b+","+"c的对手是"+c+"\n";    }}

 

【程序19】 打印出如下图案(菱形)  

*

***

******

********

******

***

*

程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重 for循环,第一层控制行,第二层控制列。  

三角形:

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
public class StartG {    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 StartG {    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("");        }    }}

 

【程序20】 有一分数序列:2/13/25/38/513/821/13...求出这个数列的前20项之和。  

程序分析:请抓住分子与分母的变化规律。

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
public class test20 {    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);        }        System.out.println(sum);    }}

更多文章,请关注公众号【程序员李木子】,有免费的电子书哦