java50道基础练习题

1.题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子, 小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少?

解析:用到递归的方法

public class Progs1 {
    public static void main(String[] args){
        //        int n=10;
//        System.out.println(fun(n));
        Scanner input=new Scanner(System.in);
        System.out.print("请输入月数:");
        int a=input.nextInt();
        System.out.println(fun(a)+"对兔子数");
    }
    private static int fun(int n){
        if(n==1||n==2)
            return 1;
        else
            return fun(n-1)+fun(n-2);


    }
}

2.题目:判断101-200之间有多少个素数,并输出所有素数。

public class Progs2 {
    /*  自己试着写的
    public static void main(String[] args){
        int i;
        for(i=101;i<=200;i++){
            if(i%2==0||i%3==0||i%5==0){
                System.out.println(i+"不是质数");
            }else{
                System.out.println(i+"是质数");
            }
        }
    }
    */

    public static void main(String[] args){

        int m = 1;

        int n = 100;

        int count = 0;

//统计素数个数

        for(int i=m;i<n;i++){

            if(isPrime(i)){

                count++;

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

                if(count%10==0){

                    System.out.println();
                }

            }

        }

        System.out.println();

        System.out.println("在"+m+"和"+n+"之间共有"+count+"个素数");

    }

//判断素数

    private static boolean isPrime(int n){
        boolean flag = true;
        if(n==1)
            flag = false;
        else{

            for(int i=2;i<=Math.sqrt(n);i++){

                if((n%i)==0 || n==1){

                    flag = false;

                    break;
                }
                else
                    flag = true;
            }

        }
        return flag;

    }
}

3.题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。 例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。

public class Progs3 {
    public static void main(String[] args){
        for(int i=153;i<1000;i++){

            if(isWaterFlawer(i))

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

        }
        System.out.println();
    }

    private static boolean isWaterFlawer(int lotus){
        int m = 0;
        int n = lotus;
        int sum = 0;
         m = n/100;
        n  -= m*100;
        sum = m*m*m;
        m = n/10;
        n -= m*10;
        sum += m*m*m + n*n*n;
        if(sum==lotus)
            return true;
        else
            return false;

    }
}

4.题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。

public class Progs4 {
    public static void main(String[] args) {
        int n = 90;
        decompose(n);
    }

    private static void decompose(int n){

        System.out.print(n+"=");

        for(int i=2;i<n+1;i++){

            while(n%i==0 && n!=i){

                n/=i;

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

            }

            if(n==i){

                System.out.println(i);

                break;

            }

        }

    }
}
5.题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
程序分析:(a>b)?a:b这是条件运算符的基本例子。
public class Progs5 {
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        System.out.print("请输入学习成绩分数:");
        int grade=input.nextInt();
        gradeNu(grade);
    }
    private static void gradeNu(int n){
        if(n<0||n>100){
            System.out.println("输入无效成绩");
        }else {
            String s=(n>=90)?"分属于A等":((n>=60)?"分属于B等":"分属于C等");
            System.out.println(n+s);
        }
    }
}

6.题目:输入两个正整数m和n,求其最大公约数和最小公倍数。

public class Progs6 {
    public static void main(String[] args){
        int a;
        System.out.print("");
        Scanner input1=new Scanner(System.in);
        System.out.println("请随便输入一个正整数A:");
        a=input1.nextInt();
        System.out.println("请随便输入一个正整数B:");
        int b=input1.nextInt();

        int m=diff(a,b);
        System.out.println(a+"的最大公约数为:"+m);
        // 计算最小公倍数 ,由于两个数的乘积等于这两个数的最大公约数与最小公倍数的积。
        int n=(a*b)/m;
        System.out.println(b+"的最小公倍数为:"+n);

    }


    //求最大公约数,用辗转法
    private static int diff(int x,int y){
        if(x<y){
            int temp=x;
            y=x;
            x=temp;
        }
        while (y!=0){
            if(x==y){
                return x;
            }else {
                int k=x%y;
                x=y;
                y=k;
            }

        }
        return x;
    }
}

7.题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

程序分析:利用while语句,条件为输入的字符不为'\n'.

public class Progs7 {
    public static void main(String[] args){
        String str="中y8? .";
        count(str);

    }
    //统计字数串每个字符的个数
    private static void count(String str){
        String E1="[\u4e00-\u9fa5]";//汉字
        String E2="[a-zA-Z]";
        String E3="[0-9]";
        String E4="\\s";//空格

        int countChinese=0;
        int countLetter=0;
        int countNumber=0;
        int countSpace=0;
        int countOthers=0;

        char[] array_char=str.toCharArray();//将字符串转化为字符数组
        String[] array_str=new String[array_char.length];//汉字只能作为字符串处理,将字符数组转化为字符串数组

        for(int i=0;i<array_char.length;i++){
            //将字符数组转化为字符串数组
            array_str[i]=String.valueOf(array_char[i]);
        }
        //遍历字符串数组中的元素
        for(String s:array_str){
            if(s.matches(E1))
                countChinese++;
            else if(s.matches(E2))
                countLetter++;
            else if(s.matches(E3))
                countNumber++;

            else if(s.matches(E4))
                countSpace++;
            else
                countOthers++;

        }
        System.out.println("输入的汉字个数:"+countChinese);

        System.out.println("输入的字母个数:"+countLetter);

        System.out.println("输入的数字个数:"+countNumber);

        System.out.println("输入的空格个数:"+countSpace);

        System.out.println("输入的其它字符个数:"+countOthers);
        
    }
}

8.题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。

public class Progs8 {
    public static void main(String[] args){

//        System.out.println(add(2,2));
        System.out.print("求s=a+aa+aaa+aaaa+...的值,请输入a的值:");
        Scanner scan=new Scanner(System.in).useDelimiter("\\s*");//以空格作为分隔符
        int a=scan.nextInt();
        int n=scan.nextInt();
        scan.close();//关闭扫描器
        System.out.println(express(a,n)+add(a,n));


    }
    //求和表达式
    private static String express(int a,int n){
        StringBuffer s=new StringBuffer();
        StringBuffer b=new StringBuffer();
        //s=s.append(a);
        for(int i=1;i<n+1;i++){
            s=s.append(a);
            b=b.append(s);
            if(i<n){
                b=b.append('+') ;
            }
        }
        b.append('=');

        return b.toString();
    }
    //求和
    private static long add(int a,int n){
        int sum=0;
        int temp=0;
        for(int i=1;i<n+1;i++){
            temp=temp*10+a;
            sum+=temp;

        }
        return sum;
    }
}

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

public class Progs9 {
    public static void main(String[] args){
//        System.out.println(compNumber(6));
        int n=10000;
        compNumber(n);
    }

    //判断是不是完美数
    private static void compNumber(int n){

        int count = 0;

        System.out.println(n+"以内的完数:");

        for(int i=1;i<n+1;i++){

            int sum = 0;

            for(int j=1;j<i/2+1;j++){

                if((i%j)==0){

                    sum += j;

                    if(sum==i){


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

                        if(count%5==0)
                            System.out.println();


                    }

                }

            }

        }

    }
}
10.题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高?
public class Progs10 {
    public static void main(String[] args){
        System.out.print("请输入小球落地时的高度和求解的次数:");
        Scanner input=new Scanner(System.in);

        int n=input.nextInt();
        double h=input.nextDouble();
        input.close();
        highMelos(n,h);

    }

    private static void highMelos(int n,double h){
//        double h=100;     //总高度
        double s=100;   //初始化一开始的米数
        for(int i=1;i<n+1;i++){

            h=h/2;
            s+=h*2;     //反弹和落下,路程得乘以2
            System.out.println("第"+i+"次反弹多高"+h);


        }
        System.out.println(s);
    }
}

11.题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

public class Progs11 {
    public static void main(String[] args){
        int z=0;
        int count=0;
        for(int i=1;i<5;i++){
            for(int j=1;j<5;j++){
                if(j==i){
                    continue;
                }
                for(int k=1;k<5;k++){
                    if(k!=i&&k!=j){
                        z=i*100+j*10+k;
                        System.out.print(z+"  ");
                        count++;
                        if(count%5==0){
                            System.out.println();
                        }

                    }
                }
            }
        }
        System.out.println();
        System.out.print("符合条件的数共:"+count);

    }

}

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,求应发放奖金总数?

public class Progs12 {
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        double w=input.nextDouble();
        System.out.println(Profit(w));
    }
    private static double Profit(double lirun){

        double prize=0;   //奖金
        long temp=0;
        if(lirun>100000&&lirun<200000){
            lirun-=100000;
            temp=100000;
            prize+=lirun*0.075;

        }else if(lirun>200000&&lirun<400000){
            lirun-=200000;
            temp=200000;
            prize+=lirun*0.05;
        }else if(lirun>400000&&lirun<600000){
            lirun-=400000;
            temp=400000;
            prize+=lirun*0.03;
        }else if(lirun>600000&&lirun<1000000){
            lirun-=600000;
            temp=600000;
            prize+=lirun*0.015;
        }else if(lirun>1000000){
            lirun-=1000000;
            temp=1000000;
            prize+=lirun*0.01;
        }
        prize+=temp*0.1;
        return prize;
    }
}

13.题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?

public class Progs13 {
    public static void main(String[] args){
        int n=0;
        int count=0;
        for(int i=0;i<10000;i++){
            if(isComf(i+100)&&isComf(i+268)){
                n=i;
                System.out.println("所求的数是:"+n);
                count++;
            }
        }
        System.out.println("10000里符合的个数为:"+count);
    }
    private static boolean isComf(int n){
        boolean flag=false;
        for(int j=1;j<Math.sqrt(n)+1;j++){
            if(n==Math.pow(j,2)){
                flag=true;
                break;
            }
        }
        return flag;
    }
}

14.题目:输入某年某月某日,判断这一天是这一年的第几天?

public class Progs14 {
    public static void main(String[] args){
        Scanner input=new Scanner(System.in).useDelimiter("\\D");
        System.out.print("请输入当前日期(年-月-日):");
        int year=input.nextInt();
        int month=input.nextInt();
        int date=input.nextInt();
        input.close();
        System.out.println("这一天是:"+year+"的第"+days(year,month,date)+"天");

    }
    //判断天数
    private static int days(int year,int month,int date){
        int n=0;
        int[] Months=new int[]{0,31,28,31,30,31,30,31,31,30,31,30};
        if(year%4==0||year%400==0&&year%100!=0){    //判断是不是闰年
            Months[2]=29;
        }
        for(int i=0;i<month;i++){
            n+=Months[i];
        }
        return n+date;
    }
}

15.题目:输入三个整数x,y,z,请把这三个数由小到大输出。

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

public class Progs15 {
    public static void main(String[] args){

        Scanner input=new Scanner(System.in).useDelimiter("\\D");
        System.out.print("输入三个整数x,y,z:");
        int x=input.nextInt();
        int y=input.nextInt();
        int z=input.nextInt();
        input.close();
        System.out.print(compare(x,y,z));

    }
    private static String compare(int a,int b,int c){
        String s=null;
        int temp=0;
        if(a>b){
            temp = a;

            a = b;

            b = temp;

        }
        if(a>c){
            temp=a;
            a=c;
            c=temp;
        }
        if(b>c){
            temp=b;
            b=c;
            c=temp;
        }
        s=a+" "+b+" "+c;
        return s;
    }
}

16.题目:输出9*9口诀。

public class Progs16 {
    public static void main(String[] args){
        pithy();
    }
    private static void pithy(){

        for(int i=1;i<10;i++){
            for(int j=1;j<i+1;j++){
                System.out.print(i+"*"+j+"="+i*j+"  ");
            }
            System.out.println();
        }
    }
}

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

public class Progs17 {
    public static void main(String[] args){
        peach();
    }
    private static void peach(){

        int sum=1;
        for(int i=9;i>0;i--){
            sum=(sum+1)*2; //第一天的桃子数是第二天桃子数加1后的2倍
        }
        System.out.print(sum);
    }
}

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

public class Progs18 {
    String a,b,c;//甲队成员
    public static void main(String[] args){
        String[] yidui=new String[]{"x","y","z"};//乙队成员
        ArrayList<Progs18> ar=new ArrayList<Progs18>();

        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                for(int k=0;k<3;k++){
                    Progs18 p18=new Progs18(yidui[i],yidui[j],yidui[k]);
                    if(!p18.a.equals(p18.b)&&!p18.b.equals(p18.c)&&!p18.a.equals(p18.c)&&!p18.a.equals("x")&&!p18.c.equals("x")&&!p18.c.equals("z")){
                        ar.add(p18);   //把符合的数据放到ArrayList里
                    }
                }
            }
        }
        for (Object ob:ar){     //遍历ArrayList
            System.out.println(ob);   //输出遍历的数据,是Object类型
        }


    }
    //构造方法
    public Progs18(String a,String b,String c){
        this.a=a;
        this.b=b;
        this.c=c;
    }
    public String toString(){       //object对象转化成string字符串输出
        return "a的对手是:"+a+"  b的对手是:"+b+"   c的对手是:"+c;
    }

}

19.题目:打印出如下图案(菱形)

public class Progs19 {
    public static void main(String[] args){
        int n=5;
        printStar(n);
    }
    //打印星星
    private static void printStar(int n){
        //打印上半部分
        int i;
        int j;
        for(i=0;i<n;i++){          //循环控制的行
            for(j=0;j<2*n;j++){    //循环控制的列,有10列
                if(j<n-i){
                    System.out.print(" ");
                }
                if(j>=n-i && j<=n+i){
                    System.out.print("*");
                }
            }
            System.out.println();
        }

打印下半部分
        for(i=1;i<n;i++){
            System.out.print(" ");
            for(j=0;j<2*n-i;j++){
                if(j<i){
                    System.out.print(" ");
                }
                if(j>=i && j<2*n-i-1){
                    System.out.print("*");
                }
            }
            System.out.println();
        }

    }

}

20.题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。

程序分析:请抓住分子与分母的变化规律。
public class Progs20 {
    public static void main(String[] args){
        int n=20;
        fensum(n);

    }
    private static void fensum(int n){

        double fenzi=2;
        double fenmu=1;
        double sum=0.0;
        double temp=0;
        double d=0.0;
        for(int i=0;i<n;i++){
            if(i==0){
                sum=fenzi/fenmu;
            }else {
                temp=fenzi;
                fenzi+=fenmu;
                fenmu=temp;
                d=fenzi/fenmu;
                sum+=d;
            }
        }

        System.out.println(sum);

    }
}

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值