一些经典java习题

1.一只青蛙在一口11米深的井底向上爬,白天向上爬3米,晚上向下滑2米,总共需要几天可以爬出.

    public class Date_1015_1 {
    
    
    private int noon;
    public static void main(String[] args) {
    
    
        int height = 0;
        int day = 1;
        int noon = 3;
        int night = 2;
        while(height != 11){
    
    
            height+=noon;
            if (height ==11){
    
    
                break;
            }
            height-= 2;
            System.out.println("距离井口还有:"+height+"米"+" "+"今天是第"+day+"天");
            day++;
        }
        System.out.println("总共需要"+day+"天爬出");
    }
}

2.猴子吃桃问题。猴子第一天摘下若干个桃子,当时就吃了一半,还不过瘾,
就又吃了一个。第二天又将剩下的桃子吃掉一半,又多吃了一个。以后每天都吃
前一天剩下的一半零一个。到第 10 天在想吃的时候就剩一个桃子了,求第一天共
摘下来多少个桃子?

package homework.Date_1015;
public class Date_1015_2 {
    
    
    public static void main(String[] args) {
    
    
        int n = 1;
        for(int day =1;day<=10;day++){
    
    
            n = (n+1)*2;
        }
        System.out.println("第一天共摘下来"+n+"个桃子");
    }
}

3.输出奇数100以内最大的5个奇数

package homework.Date_1015;
public class Date_1015_3 {
    
    
    public static void main(String[] args) {
    
    
        int a = 1;
        for(;a<=100;a++){
    
    
            if (a%2==1&&a>90){
    
    
                System.out.println(a);
            }
        }
    }
}

4.水仙花数是指一个 3 位数,它的每个位上的数字的3次幂之和等于它本身。
(例如:1^3 + 5^3 + 3^3 = 153)。编程求出1-1000以内的水仙花数。

package homework.Date_1015;
public class Date_1015_4 {
    
    
    public static void main(String[] args) {
    
    
        int d = 0;
        int n = 1;
        while(n<1000){
    
    
            int c = n%10;
            int b = (n%100)/10;
            int a = n/100;
            if(n == (a*a*a)+(b*b*b)+(c*c*c)){
    
    
                System.out.println(n+"是水仙花数");
                d++;
            }
            n++;
        }
        System.out.println("1-1000共有"+d+"个水仙花数");
    }
}

5.输出1–100之间的质数(大于1,只能被1和本身整除的,再没有其他因数的数)

package homework.Date_1015;
public class Date_1015_5 {
    
    
    public static void main(String[] args) {
    
    
        for (int i = 2; i <= 100; i++)
        {
    
    
            int j = 2;
            while (i % j != 0)
                j++;
            if (j == i)
                System.out.println(i);
        }
    }
}

6.输入某年某月某日,判断这一天是这一年的第几天?
年份主要是用来区分是否为闰年

package homework.Date_1015;
import java.util.Scanner;
public class Date_1015_6 {
    
    
    public static void main(String[] args) {
    
    
        Scanner a=new Scanner(System.in);
        System.out.println("请输入年月日");
        int year=a.nextInt();
        int month=a.nextInt();
        int day=a.nextInt();
        int n=0,Month=0;
        for(int i=1;i<=month;i++)
        {
    
    
            switch(i)
            {
    
    
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:Month+=31;break;
                case 4:
                case 6:
                case 9:
                case 11:Month+=30;break;
                case 2:
                    if(year%100==0&&year%400==0||year%4==0)
                    {
    
    
                        Month+=29;
                    }
                    else
                    {
    
    
                        Month+=28;
                    }
                    break;
            }
        }
        n=Month+day;
        System.out.println("今天是今年的第"+n+"天");
    }
}

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

package homework.Date_1015;
public class Date_1015_7 {
    
    
    public static void main(String[] args) {
    
    
        int a = 1,sum = 1;
        int month = 12;
        int b;
        for(int i=1;i<=month;i++){
    
    
            if(i<=2){
    
    
                System.out.println("第"+i+"个月的兔子有:1对");
            }else{
    
    
                b = sum;
                sum = a+sum;
                a = b;
                System.out.println("第"+i+"个月的兔子有:"+sum+"对");
            }
        }
    }

}

8.用户输入两个数,求两个数的最大公约数,最小公倍数

package homework.Date_1015;
import java.util.Scanner;
public class Date_1015_8 {
    
    
    public static void main(String args[])
    {
    
    
        Scanner s = new Scanner(System.in);
        System.out.println("请输入两个数");
        int a = s.nextInt();
        int b = s.nextInt();
        int c = 0,d = 0;
        if(a>b)
        {
    
    
            c = b;
        }
        else
        {
    
    
            c = a;
        };
        for(int i=1; i<=c; i++)
        {
    
    
            if(a%i == 0&&b%i == 0)
            {
    
    
                d = i;
            }
        }
        System.out.println("最大公约数为: " + d);
        System.out.println("最小公倍数为: " + a*b/d);    }
}

猜你喜欢

转载自blog.csdn.net/weixin_45636230/article/details/109189056