你能想到的 JAVA 经典算法题

又把算法题敲了一遍,以后看时能方便些吧

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

public class test1 {

    public static void main (String args[]){
        int i=1;
        int a=1;
        Scanner scanner = new Scanner(System.in);
        System.out.println ("输入第几月");
        int month = Integer.valueOf(scanner.nextLine());
        for (; a <= month ; a++){
            i = f(a);
        }
        System.out.print(i);
    }
    public static int f(int x ){
        if (x==1 || x==2 ){
            return 1;
        }else {
            return f(x-1)+f(x-2);
        }
    }
}

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

public class test2 {
    public static void main(String  args[]) {
        int a ;
        int num = 0 ;
        boolean flage ;
        for (a = 101; a<=200; a++){
            flage = true;
            if (a%2 == 0 ){
                continue;
            }
            for(int b = 2; b < a/2 ;b++){
                if(a%b == 0){
                    flage = false;
                    break;
                }
            }
            if(flage){
                 num++;
                 System.out.println(a);
            }
        }
        System.out.println("总数"+"\n"+num+"个");
        
    }
}

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

public class test3 {
    public static void main(String args[]) {
    int a ;
    int b ;
    int c ;
    int i ;
     for (i=100; i <=999; i++){
         a = i/100;
         b = (i%100)/10;
         c = (i%100)%10;
         if (a*a*a+b*b*b+c*c*c == i){
             System.out.println(i);
         }
     }
    }
}

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

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

        Scanner scanner = new Scanner(System.in);
        System.out.print("输入:");
        String a = scanner.nextLine();
        Integer b = Integer.valueOf(a);
        System.out.print("分解质因数:" + b + "=");
        text(b);
    }
    private static void text(int b) {
        
        for ( int i = 2; i < b/2; i++){
            while(b!=i){
            if (b%i == 0){
                System.out.print(i+"*");
                b=b/i;
            }else{
                break;
            }
          }
        }
        System.out.print(b);
    }
}

题目:利用条件运算符的嵌套来完成此题:学习成绩> =90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。

public class test5 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入:");
        String a = scanner.nextLine();
        Double b = Double.valueOf(a);
        String c = (b >= 90?"A":( b < 60 ? "C":"B"));
        System.out.println(c);
    }
}

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

更相减损法

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入a:");
        int a = Integer.valueOf(scanner.nextLine());
        System.out.println("输入b:");
        int b = Integer.valueOf(scanner.nextLine());
        int d = 0;
        Boolean flage = false;
       Map<String, Object> map = new HashMap<String, Object>();
        map.put("a", a);
        map.put("b", b);
        map.put("d", d);
        map.put("flag", flage);
        map = f(map);
        Boolean c = (Boolean) map.get("flag");
        if (c) {
            a = (int) map.get("a");
            b = (int) map.get("b");
            d = (int) map.get("d");
            int e = t(a, b);
            System.out.print("最大公约数"+e * 2 * d);
        }

        if (!c) {
            a = (int) map.get("a");
            b = (int) map.get("b");
            int e = t(a, b);
            System.out.print("最大公约数"+e);
        }
        
    }

    private static int t(int a, int b) {
        if (a > b) {
            a = a - b;
            return t(a, b);
        } else if (a < b) {
            b = b - a;
            return t(a, b);
        }

        return a;
    }

    private static Map<String, Object> f(Map<String, Object> map) {
        int a1 = (int) map.get("a");
        int b1 = (int) map.get("b");
        int d1 = (int) map.get("d");
        Boolean flag1 = (Boolean) map.get("flag");
        while (a1 % 2 == 0 && b1 % 2 == 0) {
            flag1 = true;
            d1++;
            map.put("a", a1 / 2);
            map.put("b", b1 / 2);
            map.put("d", d1);
            map.put("flag", flag1);
            return f(map);

        }

        return map;

}

辗转相除法

public class test6 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入a:");
        int a = Integer.valueOf(scanner.nextLine());
        System.out.println("输入b:");
        int b = Integer.valueOf(scanner.nextLine());
        int x = f(a, b);
        System.out.println("最大公约数"+ x);
        int f = a * b / x;
        System.out.println("最小公倍数为" + f);

    }

    public static int f(int x, int y) {
        int flag;
        if (x < y) {
            flag = x;
            x = y;
            y = flag;
        }
        while (y != 0) {
            int d = x % y;
            x = y;
            y = d;
        }
        return x;
    }

}

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

public class test7 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入数字:");
        int a = Integer.valueOf(scanner.nextLine());
        System.out.println("输入数字个数:");
        int b = Integer.valueOf(scanner.nextLine()); 
        int c = 0;
        for (int i = 1; i <= b; i++) {

             c =(int) (c + a*Math.pow(10, i-1)) + c;
        }
        
        System.out.println("结果:"+ c );

    }
}

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

public class test8 {

    public static void main(String[] args) {
        for (int a = 2; a <= 1000; a++) {
            int c = 0;
            for (int b = 1; b <= a / 2; b++) {
                if (a % b == 0) {
                    c = c + b ;
                }
            }
            if (c == a) {
                System.out.println(a);
            }
        }
    }
}

题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高?

public class test9 {

    public static void main(String[] args) {
        float a = 100;
        float c = 0;
        for (int b = 1; b <= 10; b++) {
            c = c + a;
            a = a / 2;
        }
        System.out.println(a);
        System.out.print(c);
    }
}

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

public class test10 {

    public static void main(String[] arge) {
        int n = 0;
        for (int a = 1; a <= 4; a++) {
            for (int b = 1; b <= 4; b++) {
                for (int c = 1; c <= 4; c++) {
                    if (a!=b && a!=c && b!=c){
                        n++;
                        System.out.println(a*100+b*10+c);
                    }
                }
            }
        }
        System.out.println("总数"+n);
    }
}

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

public class test12 {
    public static void main(String[] arge) {
        int c = 0;
        int d = 0;
        for (int a = 1; a < 100000; a++) {
            for (int b = 1; b < 10000; b++) {
                if (a + 100 == b * b) {
                    for (int e = b; e < 10000; e++) {
                        if (a + 168 == e * e) {
                            System.out.print(a);
                        }
                    }
                }
            }
        }
    }
}

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

public class test13 {
    public static void main(String[] arge) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入日期:");
        String date = scanner.nextLine();
        String[] str = date.split("-");
        int year = Integer.valueOf(str[0]);
        int month = Integer.valueOf(str[1]);
        int day = Integer.valueOf(str[2]);
        int twoMonth = 0;
        int days = 0;
        if ((year % 4 == 0 && year % 100 == 0) || (year % 400 == 0)) {
            twoMonth = 29;
        } else {
            twoMonth = 28;
        }
        int[] months = { 31, twoMonth, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        for (int a = 0; a < months.length; a++) {
            if (month >= a + 1) {
                if (month == a + 1) {
                    days += day;
                    break;
                }
                days += months[a];
            } else {
                System.out.println("日期格式错误");
            }
        }
        System.out.println("第" + days + "天");
    }
}

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

public class test14 {
    public static void main(String[] arge) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入1:");
        int x = Integer.valueOf(scanner.nextLine());
        System.out.println("输入2:");
        int y = Integer.valueOf(scanner.nextLine());
        System.out.println("输入3:");
        int z = Integer.valueOf(scanner.nextLine());
        if (x >= y) {
            int temp = x;
            x = y;
            y = temp;
        }
        if (x >= z) {
            int temp1 = x;
            x = z;
            z = temp1;
        }
        if (y >= z) {
            int temp2 = y;
            y = z;
            z = temp2;
        }
        System.out.println("输入:" + x + "-" + y + "-" + z);
    }
}

题目:输出9*9口诀。

public class test15 {
    public static void main(String[] arge) {
        for (int a = 1; a <= 9; a++) {
            for (int b = 1; b <= a; b++) {
                System.out.print(b + "*" + a + "=" + (a * b) + "    ");
            }
            System.out.println();
        }
    }
}

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

public class test16 {
    public static void main(String[] arge) {
        
          int num = 1; 
          for(int a = 9; a >= 1;a--)
          { 
              num =(num+1)*2; 
          }
         System.out.print(num);

 }

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

public class test17 {

    public static void main(String[] args) {
        char a, b, c;
        for (a = 'x'; a <= 'z'; a++) {
            for (b = 'x'; b <= 'z'; b++) {
                if (a != b) { 
                    for (c = 'x'; c <= 'z'; c++) {
                        if (a != c && b != c) { 
                            if (a != 'x' && c != 'x' && c != 'z') { 
                                System.out.println("a和" + a + ",b和" + b + ",c和" + c + "进行比赛");
                            }
                        }
                    }
                }
            }
        }
    }
}

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

 *   
 ***   
 ******   
********   
******   
***   
*  

public class test18 {
    public static void main(String[] args) {
        // 半菱形

        for (int a = 1; a <= 4; a++) {
            for (int b = 1; b <= 2 * a - 1; b++) {
                System.out.print("*");
            }
            System.out.println();
        }
        for (int a = 3; a >= 1; a--) {
            for (int b = 2 * a - 1; b >= 1; b--) {
                System.out.print("*");
            }
            System.out.println();
        }

        // 菱形

        for (int a = 1; a <= 4; a++) {
            for (int c = 1; c <= 4 - a; c++) {
                System.out.print(" ");
            }
            for (int b = 1; b <= 2 * a - 1; b++) {
                System.out.print("*");
            }
            System.out.println();
        }
        for (int a = 1; a <= 3; a++) {
            for (int b = 1; b <= a; b++) {
                System.out.print(" ");
            }
            for (int c = 5; c >= 2 * a - 1; c--) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

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

public class test19 {
    public static void main(String[] args) {
        double x = 1;
        double y = 2;
        double num = 0;
        for (int a = 1; a <= 20; a++) {
            num += y/x;
            double z= y;
             y = x+y;
             x = z; 
        }
        System.out.print(num);
    }
}

题目:求1+2!+3!+...+20!的和

public class test20 {
    public static void main(String[] args) {
        
        long number = 0;
        for (int a = 1; a <= 20; a++) {
            long num = 1;
            for (int b = 1; b <= a; b++) {
                num =  num * b ;
            }
            number +=num;
        }
        System.out.print(number);
    }
}

题目:利用递归方法求5!


public class test21 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("输入");
        int a = Integer.valueOf(scanner.nextLine());
        int b = f(a);
        System.out.print(b);
    }

    private static int f(int a) {
        int num = 1;
        if (a == 1 || a == 0) {
            return 1;
        }else if (a > 0) {
            num = f(a-1)*a;
        }
        return num;
    }
}

题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个人多大?

public class test22 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner (System.in);
        System.out.print("请问第几个人:") ;
        int a = Integer.valueOf(scanner.nextLine());
        System.out.println("第"+a+"个人:") ;
        int b = f(a);
        System.out.print(b);
    }

    private static int f(int a) {
        int c ;
        if (a == 1) {
            return 10;
        }else  {
            c = f(a-1)+2;
        }
        return c;
    }
}

题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。

public class text23 {
    public static void main(String[] args) {
        int a = 12345;
        int d = 5;
        for (int i = 1; i <= d; i++) {
            float b = (float) (a / Math.pow(10, i));
            if (b < 1) {
                System.out.println(i + "位数");
                break;
            }
        }
        String str = String.valueOf(a);
        int[] arr = new int[str.length()];
        for (int x = 0; x < arr.length; x++) {
            char ch = str.charAt(x);
            arr[arr.length - 1 - x] = Integer.valueOf(String.valueOf(ch));
        }
        for (int y = 0; y <= arr.length - 1; y++) {
            System.out.print(arr[y]);
        }
    }
}

题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。

public class test24 {
    public static void main(String[] args) {
        int a = 21632;
        if (a > 99999 || a < 10000) {
            System.out.println("error");
        }
        String str = String.valueOf(a);
        char[] ch = str.toCharArray();
        if (ch[0] == ch[4]) {
            if (ch[1] == ch[3]) {
                System.out.print("YES");
            } else {
                System.out.print("NO");
            }
        } else {
            System.out.print("NO");
        }
    }
}

题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续 判断第二个字母。

public class test25 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("输入第1个字母");
        String fir = scanner.nextLine();
        char[] ch = fir.toCharArray();
        char weekFir = ch[0];
        switch (weekFir) {
        case 'M':
        case 'm':
            System.out.print("星期1");
            break;
        case 'T':
        case 't':
            System.out.print("输入第2个字母");
            String sec = scanner.nextLine();
            char[] t = sec.toCharArray();
            char weekSec = t[0];
            switch(weekSec){
            case 'U':
            case 'u':
                System.out.print("星期2");
                break;
            case 'H':
            case 'h':
                System.out.print("星期4");
                break;
            default :
                System.out.print("没找到匹配日期,或者输入有误");
            }
            break;
        case 'W':
        case 'w':
            System.out.print("星期3");
            break;
        case 'F':
        case 'f':
            System.out.print("星期5");
            break;
        case 'S':
        case 's':
            System.out.print("输入第2个字母");
            String second = scanner.nextLine();
            char[] s = second.toCharArray();
            char weekSecond = s[0];
            switch(weekSecond){
            case 'A':
            case 'a':
                System.out.print("星期6");
                break;
            case 'U':
            case 'u':
                System.out.print("星期7");
                break;
            default :
                System.out.print("没找到匹配日期,或者输入有误");
            }
            break;
        default :
            System.out.print("没找到匹配日期,或者输入有误");
        }
    }
}

题目:求100之内的素数

public class test26 {
    public static void main(String[] args) {
        for (int x = 2; x <= 100; x++) {
            int count = 0;
            if (!(x % 2 == 0) || x == 2) {
                for (int y = 2; y <= x / 2; y++) {
                    if ((x % y == 0)) {
                        count++;
                    }
                }
                if (count == 0) {
                    System.out.println(x);
                }
            }
        }
    }
}

题目:对10个数进行排序

public class test27 {
    public static void main(String[] args) {
        int[] arr = { 2, 5, 6, 9, 7, 4, 3, 11, 25, 13};
        int temp ;
        for (int x = 0; x < 10; x++) {
            for (int y = x + 1; y <= 9; y++) {
                if (arr[x] > arr[y]){
                    temp = arr[y];
                    arr[y] = arr[x];
                    arr[x] = temp;
                }
            }
            System.out.println(arr[x]);
        }
    }
}

题目:求一个3*3矩阵对角线元素之和

public class test28 {
    public static void main(String[] args) {
        int[][] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
        int sum = 0;
        for (int x = 0; x < arr.length; x++) {
            for (int y = 0; y < arr[x].length; y++) {
                if (x == y ) {
                    sum += + arr[x][y];
                }
                if (x == arr[x].length -1 - x ) {
                    sum += + arr[x][y];
                }
            }
        }
        System.out.print(sum);
    }
}

题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。

public class test29 {
    public static void main(String[] args) {
        int[] arr = new int[] { 2, 5, 6, 9, 7, 4, 3, 11, 25, 13 };
        int temp;
        for (int x = 0; x < 10; x++) {
            for (int y = x + 1; y <= 9; y++) {
                if (arr[x] > arr[y]) {
                    temp = arr[y];
                    arr[y] = arr[x];
                    arr[x] = temp;
                }
            }
            System.out.print(arr[x] + " ");
        }
        Scanner scanner = new Scanner(System.in);
        System.out.print("输入");
        int number = Integer.valueOf(scanner.nextLine());
        int[] newArr = new int[arr.length + 1];
        if (number >= arr[arr.length - 1]) {
            newArr[newArr.length - 1] = number;
            for (int z = 0; z < newArr.length - 1; z++) {
                newArr[z] = arr[z];
            }
        }
        for (int a = 0; a < arr.length ; a++) {
            if (number < arr[a]) {
                newArr[a] = number;
                for (int z = a; z < arr.length ; z++) {
                    newArr[z + 1] = arr[z];
                }
                for (int c = 0; c < a ;c++) {
                    newArr[c] = arr[c];
                }
                break;        
            }
        }
        for (int b = 0; b < newArr.length ; b++) {
            System.out.print(newArr[b] + " ");
        }
    }
}

题目:将一个数组逆序输出。

public class test30 {
    public static void main(String[] args) {
        int[] arr = { 2, 5, 6, 9, 7, 4, 3, 11, 25};
        for (int x = 0; x < arr.length / 2; x++) {
            int temp;
            temp = arr[x];
            arr[x] = arr[arr.length - 1 - x];
            arr[arr.length - 1 - x] = temp;
        }
        for (int y = 0; y < arr.length; y++) {
            System.out.print(arr[y] + " ");
        }
    }
}

题目:取一个整数a从右端开始的4~7位。

public class test31 {
    public static void main(String[] args) {
        String x = String.valueOf(1234567890);
        char [] ch = x.toCharArray();
        System.out.print(ch[ch.length-4]);
        System.out.print(ch[ch.length-5]);
        System.out.print(ch[ch.length-6]);
        System.out.print(ch[ch.length-7]);
    }
}

题目:打印出杨辉三角形

1   
1   1   
1   2   1   
1   3   3   1   
1   4   6   4   1   
1   5   10   10   5   1 

public class test32 {
    public static void main(String[] args) {
        int[][] x = new int[6][];
        for (int y = 0; y < x.length; y++) {
            x[y] = new int[y + 1];
        }
        for (int z = 0; z < x.length; z++) {
            for (int a = 0; a < x[z].length; a++) {
                x[z][0] = 1;
                if (z == a) {
                    x[z][a] = x[z][0];
                    break;
                }
                if (z > 1 && a >= 1) {
                    x[z][a] = x[z - 1][a - 1] + x[z - 1][a];
                }
            }
        }
        for (int b = 0; b < x.length; b++) {
            for (int c = 0; c <= b; c++) {
                System.out.print(x[b][c] + "\t" );
            }
            System.out.println();
        }
    }
}

题目:输入3个数a,b,c,按大小顺序输出。

public class test33 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("输入第1个正整数");
        int a = Integer.valueOf(scanner.nextLine());
        System.out.print("输入第2个正整数");
        int b = Integer.valueOf(scanner.nextLine());
        System.out.print("输入第3个正整数");
        int c = Integer.valueOf(scanner.nextLine());
        int[] x = { a, b, c };
        for (int i = 0; i < x.length; i++) {
            for (int y = i + 1; y < x.length; y++) {
                if (x[i] > x[y]) {
                    int temp = x[i];
                    x[i] = x[y];
                    x[y] = temp;
                }
            }
        }
        for (int z = 0; z < x.length; z++) {
            System.out.print(x[z] + "\t");
        }
    }
}

题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。

public class test34 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("数组长度:");
        int len = Integer.valueOf(scanner.nextLine());
        int[] arr = new int[len];
        for (int i = 1; i <= len; i++) {
            System.out.println("输入第" + i + "个数");
            int a = Integer.valueOf(scanner.nextLine());
            arr[i - 1] = a;
        }
        int max = 0;
        int min = 0;
        for (int x = 0; x < arr.length; x++) {
            if (arr[x] > arr[max]) {
                max = x;
            }
            if (arr[x] < arr[min]) {
                min = x;
            }
        }
        int temp = arr[0];
        arr[0] = arr[max];
        arr[max] = temp;
        int tempp = arr[arr.length - 1];
        arr[arr.length - 1] = arr[min];
        arr[min] = tempp;
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + ",");
        }
    }
}

题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数

public class test35 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入整数个数:");
        int len = Integer.valueOf(scanner.nextLine());
        int[] arr = new int[len];
        for (int x = 1; x <= len; x++) {
            System.out.println("输入第" + x + "个数");
            int a = Integer.valueOf(scanner.nextLine());
            arr[x - 1] = a;
        }
        for (int x = 0; x < len; x++) {
            System.out.print(arr[x] + "\t");
        }
        System.out.println();
        System.out.println("后移位置数");
        int b = Integer.valueOf(scanner.nextLine());
        if (b <= len) {
            int[] newArr = new int[b];
            for (int x = 0; x < b; x++) {
                newArr[x] = arr[len - b + x];
            }
            for (int x = len - 1; x >= b; x--) {
                arr[x] = arr[x - b];
            }
            for (int x = 0; x < newArr.length; x++) {
                arr[x] = newArr[x];
            }
            System.out.print("输出结果");
            System.out.println();
            for (int x = 0; x < arr.length; x++) {
                System.out.print(arr[x] + "\t");
            }
        } else {
            System.out.print("后移位数不能大于数组长度!!!!!!!");
        }
    }
}

题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。

public class test36 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入人数:");
        int n = Integer.valueOf(scanner.nextLine());
        int[] arr = new int[n+1];
        for (int i = 1; i <= n; i++) {
            arr[i] = 1;
        }
        int flag = 0;
        int count = n;
        for (int i = 1;; i++) {
            if (i == n+1 ) {
                i = 1;
            }
            if (arr[i] != 0) {
                flag++;
            } else {
                continue;
            }
            if (flag % 3 == 0) {
                arr[i] = 0;
                count--;
            }
            if (count == 1) {
                break;
            }
        }
        for (int i = 1; i <= n; i++) {
            if (arr[i] != 0) {
                System.out.print(i);
            }
        }
    }
}

题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。

public class test37 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入字符串:");
        String str = scanner.nextLine();
        char [] ch = str.toCharArray();
        System.out.print(ch.length);
    }        
}

题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n

public class test38 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入数字:");
        int number = Integer.valueOf(scanner.nextLine());
        System.out.print(f(number));
    }

    private static double f(int number) {
        double num = 0d;
        // TODO Auto-generated method stub
        if (number % 2 == 0) {
            for (int i = 2; i <= number;) {
                num += 1.0 / i;
                i = i + 2;
            }
        }
        if (number % 2 != 0) {
            for (int i = 1; i <= number;) {
                num += 1.0 / i;
                i = i + 2;
            }
        }
        return num;
    }
}

题目:字符串排序。

public class test39 {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("ayfg");
        list.add("dugj");
        list.add("fghg");
        list.add("ouyt");
        list.add("bkfe");
        list.add("akfe");
        System.out.println(list);
        Collections.sort(list);
        System.out.print(list);
        
    }
}

题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?

public class test40 {
    public static void main(String[] args) {
        for (int i = 1;;i++) {
            int num = i;
            if ((num - 1) % 5 == 0) {
                num = (num - 1) / 5 * 4 ;
                if ((num - 1) % 5 == 0) {
                    num = (num - 1) / 5 * 4 ;
                    if ((num - 1) % 5 == 0) {
                        num = (num - 1) / 5 * 4 ;
                        if ((num - 1) % 5 == 0) {
                            num = (num - 1) / 5 * 4 ;
                            if ((num - 1) % 5 == 0) {
                                System.out.print(i);
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
}

   题目:公鸡5文钱1只,母鸡3文钱1只,小鸡3只1文钱,用100文钱买100只鸡,可以怎么买

public class test41 {
    public static void main(String[] args) {
        int cost ;
        for (int x = 0; x <= 20; x++) {
            for (int y = 0; y <= 33 ; y++) {
                int z = 100-x-y;
                if (z % 3 == 0) {
                    cost = 5*x + 3*y + z/3;
                    if (cost == 100) {
                        System.out.println("公鸡的数量为:"+x+",母鸡的数量为:"+y+",小鸡的数量为:"+z);
                    }
                }
            }
        }
    }
}
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值