Javase 39 basic exercises

JavaSE 39 basic exercises

This article is to help learn the basic Java program. I hope you can practice. I typed the code myself. I hope you can have your own program ideas. Please forgive me for your shortcomings.

1. Fibonacci sequence

Title: classical question: a pair of rabbits give birth to a pair of rabbits every month from the third month after birth. The little rabbit grows to another pair of rabbits every month after the third month. If the rabbits don't die, what are the logarithm of rabbits every month?

Program analysis: the law of rabbits is the sequence 1, 1, 2, 3, 5, 8, 13, 21... That is, the Fibonacci sequence.

package com.Pract;

import java.util.Scanner;

public class LianXi50 {
    public static int fun(int n) {
        if (n == 1 || n == 2)
            return 1;
        else
            return fun(n - 1) + fun(n - 2);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int mouth = scanner.nextInt();
        int rabbit = fun(mouth);
        System.out.println(rabbit);
    }
}

2. Prime number

Topic: judge how many primes there are between 101-200 and output all primes.

Program analysis: method of judging prime numbers: use a number to remove 2 to sqrt (this number) respectively. If it can be divided, it indicates that this number is not a prime number, on the contrary, it is a prime number.

package com.Pract;

public class LianXi50 {
    public static void fun(int n, int m) {
        int count = 0;
        while (n <= m) {
            int i = 2;
            for (i = 2; i < (int) Math.sqrt(n); i++) {
                if (n % i == 0)
                    break;
            }
            if (i == (int) Math.sqrt(n)) {
                count++;
                System.out.print(n + " ");
            }
            n++;
        }
        System.out.println("\n" + count);
    }

    public static void main(String[] args) {
        fun(101, 200);
    }
}

3. Daffodils number

Title: print out all the "daffodils". The so-called "daffodils" refers to a three digit number, and the sum of each digit cube is equal to the number itself. For example, 153 is a "daffodil number", because 153 = the third power of 1 + the third power of 5 + the third power of 3.

Program analysis: use the for loop to control 100-999 numbers, and each number is decomposed into bits, tens and hundreds.

package com.Pract;

public class LianXi50 {
    public static int shuxianhua(int n) {
        int sum = 0;
        while (n != 0) {
            sum += Math.pow(n % 10, 3);
            n /= 10;
        }
        return sum;
    }

    public static void fun() {
        int i = 100;
        while (i < 1000) {
            if (shuxianhua(i) == i)
                System.out.print(i + " ");
            i++;
        }
    }

    public static void main(String[] args) {
        fun();
    }
}

4. Decomposition quality factor

Topic: decompose a positive integer into prime factors. For example, enter 90 and print out 90 = 2 * 3 * 3 * 5.

Program analysis: to decompose the prime factor of n, first find a minimum prime number k, and then complete the following steps:

(1) If the prime number is exactly equal to n, it means that the process of decomposing the prime factor has ended, and you can print it out.

(2) If n < > k, but n can be divided by K, print out the value of K, divide n by the quotient of K as a new positive integer n, and repeat the first step.

(3) If n cannot be divided by k, repeat the first step with k+1 as the value of k.

package com.Pract;

import java.util.Scanner;

public class LianXi50 {
    public static void fun(int n) {
        int k = 0;
        for (int i = 2; i < n; i++) {
            if (n % i == 0) {
                k = i;
                break;
            }
        }
        String str = n + "=";
        while (k < n) {
            if (n % k == 0) {
                str += k + "*";
                n /= k;
            } else {
                k++;
            }
        }
        char[] c = str.toCharArray();
        String str2 = new String(c, 0, c.length - 1);
        System.out.println(str2);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        fun(scanner.nextInt());
    }
}

5. Grade

Title: use the nesting of conditional operators to complete this question: students with academic achievement > = 90 points are represented by A, those between 60-89 points are represented by B, and those below 60 points are represented by C.

Program analysis: (a > b)? a: B this is a basic example of conditional operators.

package com.Pract;

import java.util.Scanner;

public class LianXi50 {
    public static void fun(int grade) {
        System.out.println(grade >= 90 ? 'A' : grade >= 60 ? 'B' : 'C');
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            fun(scanner.nextInt());
        }
    }
}

6. Maximum common factor and minimum common multiple

Title: enter two positive integers m and n to find their maximum common divisor and minimum common multiple.

Program analysis: rolling method is used.

package com.Pract;

import java.util.Scanner;

public class LianXi50 {
    public static void fun(int m, int n) {
        int a = m;
        int b = n;
        int gys = 0;
        if (a > b) {
            int t = a;
            a = n;
            b = t;
        }
        if (b % a == 0)
            gys = b;

        while (a != 0) {
            int t = a;
            a = b % a;
            b = t;
        }
        gys = b;
        int gbs = m * n / gys;
        System.out.println(gys + " " + gbs);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            fun(scanner.nextInt(), scanner.nextInt());
        }
    }
}

7. Statistical character

Title: input a line of characters and count the number of English letters, spaces, numbers and other characters.

Program analysis: using the while statement, the condition is that the input character is not '\ n'

package com.Pract;

import java.util.Scanner;

public class LianXi50 {
    public static void fun(String str) {
        int ec = 0;
        int sc = 0;
        int dc = 0;
        int oc = 0;
        for (char c : str.toCharArray()) {
            if ('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z')
                ec++;
            else if (c == ' ')
                sc++;
            else if ('0' <= c && c <= '9')
                dc++;
            else
                oc++;
        }
        System.out.println(ec + " " + sc + " " + dc + " " + oc);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        fun(scanner.nextLine());
    }
}

8. Find s=a+aa+aaa+aaaa+aa... A

Title: find the value of s=a+aa+aaa+aaaa+aa... A, where a is a number. For example, 2 + 22 + 222 + 2222 + 22222 = 24690 (at this time, a total of 5 numbers are added), and the addition of several numbers is controlled by keyboard.

Program analysis: the key is to calculate the value of each item.

package com.Pract;

import java.util.Scanner;

public class LianXi50 {
    public static void fun(int a, int b) {
        int sum1 = 0;
        int sum2 = 0;
        int i = 0;
        int t = a;
        while (i < b) {
            t = a * (int) Math.pow(10, i);
            sum1 += t;
            sum2 += sum1;
            i++;
        }
        System.out.println(sum2);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        fun(scanner.nextInt(), scanner.nextInt());
    }
}

9. Completion

Title: if a number is exactly equal to the sum of its factors, the number is called "perfect". For example, 6 = 1 + 2 + 3 Program to find all completions within 1000.

package com.Pract;

public class LianXi50 {
    public static int wanshu(int n) {
        int sum = 0;
        for (int i = 1; i < n; i++) {
            if (n % i == 0)
                sum += i;
        }
        return sum;
    }

    public static void fun() {
        for (int i = 2; i <= 1000; i++) {
            if (wanshu(i) == i)
                System.out.print(i + " ");
        }
    }

    public static void main(String[] args) {
        fun();
    }
}

10. Free fall

Title: a ball falls freely from a height of 100 meters and jumps back to half of the original height after each landing; How many meters will it pass on the 10th landing? How high is the 10th rebound?

Program analysis: proportional series

package com.Pract;

import java.util.Scanner;

public class LianXi50 {
    public static void fun(int n) {
        double a = 100 * Math.pow(0.5, n - 1);
        double s = 100 * (1 - Math.pow(0.5, n)) / (1 - 0.5);
        System.out.println(a + " " + (s * 2 - 100));
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        fun(scanner.nextInt());
    }
}

11. Combinatorial sorting

Title: there are 1, 2, 3 and 4 numbers. How many different three digits can be formed without repeated numbers? How much is it?

Program analysis: the numbers that can be filled in hundreds, tens and single digits are 1, 2, 3 and 4. After all permutations are formed, the permutations that do not meet the conditions are removed.

package com.Pract;

public class LianXi50 {
    public static void fun() {
        for (int i = 1; i < 5; i++) {
            for (int j = 1; j < 5; j++) {
                if (i == j)
                    continue;
                for (int k = 1; k < 5; k++) {
                    if (k == j || i == k)
                        continue;
                    System.out.print(i + "" + j + "" + k+" ");
                }
            }
        }
    }

    public static void main(String[] args) {
        fun();
    }
}

12. Enterprise profit

Title: the bonus paid by the enterprise is based on the profit commission. When the profit (I) is less than or equal to 100000 yuan, the bonus can be increased by 10%; When the profit is higher than 100000 yuan and lower than 200000 yuan, the part lower than 100000 yuan will be deducted by 10%, and the part higher than 100000 yuan will be deducted by 7.5%; Between 200000 and 400000 yuan, 10% commission will be given for the part lower than 200000 yuan, and 5% commission will be given for the part higher than 200000 yuan; Between 400000 and 600000 yuan, 10% will be deducted for the part less than 400000 yuan, and 3% will be deducted for the part higher than 400000 yuan; Between 600000 and 1 million yuan, 10% commission will be given for the part lower than 600000 yuan, 1.5% commission will be given for the part higher than 600000 yuan, 10% commission will be given for the part lower than 1 million yuan and 1% commission will be given for the part higher than 1 million yuan. Input the profit I of the current month from the keyboard to calculate the total amount of bonus to be paid?

Program analysis: please use the number axis to divide and locate. Note that the bonus should be defined as a growth integer.

package com.Pract;

import java.util.Scanner;

public class LianXi50 {
    public static void fun(double n) {
        double price;
        n /= 10000;
        if (n <= 10)
            price = n * 0.1;
        else if (n <= 20)
            price = 10 * 0.1 + (n - 10) * 0.075;
        else if (n <= 40)
            price = 20 * 0.1 + (n - 20) * 0.05;
        else if (n <= 60)
            price = 40 * 0.1 + (n - 40) * 0.03;
        else if (n <= 100)
            price = 60 * 0.1 + (n - 60) * 0.015;
        else
            price = 100 * 0.1 + (n - 100) * 0.01;
        System.out.println(price * 10000);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true)
            fun(scanner.nextInt());
    }
}

13. Special perfect square

Title: an integer, which is a complete square number after adding 100, and a complete square number after adding 168. What is the number? The answer is 21.

Program analysis: first add 100 to the number before square, and then add 268 to the number before square. If the results after square are integers, it is the result.

package com.Pract;

public class LianXi50 {
    public static void fun() {
        int i = 0;
        while (true) {
            double t1 = Math.sqrt(i + 100);
            double t2 = Math.sqrt(i + 268);
            if (t1 % 1 != 0 || t2 % 1 != 0)
                i++;
            else {
                System.out.println(i);
                break;
            }
        }
    }

    public static void main(String[] args) {
        fun();
    }
}

14. Days calculation

Title: enter a certain day of a certain month of a certain year (in the format of xxxx.xx.xx), and judge which day of the year this day is?

Program analysis: establish leap year array and normal year array.

package com.Pract;

import java.util.Scanner;

public class LianXi50 {
    public static void fun(String str) {
        int[] leapYear = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int[] commonYear = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        String[] date = str.split("\\."); // Note escape characters
        int year = Integer.valueOf(date[0]);
        int mouth = Integer.valueOf(date[1]);
        int day = Integer.valueOf(date[2]);
        for (int m = 0; m < mouth; m++) {
            if (year % 4 == 0 && year % 100 != 0)
                day += leapYear[m];
            else
                day += commonYear[m];
        }
        System.out.println(day);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        fun(scanner.nextLine());
    }
}

15. Three number comparison size

Title: enter three integers x, y and Z. please output these three numbers from small to large.

Program analysis: we try to put the smallest number on X, first compare x with y, if x > y, exchange the values of X and y, and then compare x with z. if x > z, exchange the values of X and z, so as to minimize X.

package com.Pract;

import java.util.Scanner;

public class LianXi50 {
    public static void fun(int... a) {
        for (int i = 0; i < a.length; i++) {
            for (int j = i; j < a.length - 1; j++) {
                if (a[j] > a[j + 1]) {
                    int t = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = t;
                }
            }
        }
        System.out.println(a[0] + " " + a[1] + " " + a[2]);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        fun(scanner.nextInt(), scanner.nextInt(), scanner.nextInt());
    }
}

16. 9 * 9 formula

Title: output 9 * 9 formula.

Procedure analysis: Considering branches and columns, there are 9 rows and 9 columns in total, i control row and j control column.

package com.Pract;

public class LianXi50 {
    public static void fun() {
        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();
        }
    }

    public static void main(String[] args) {
        fun();
    }
}

17. Monkeys steal peaches

Topic: monkeys eat peaches: on the first day, monkeys picked several peaches and ate half of them immediately. They were not addicted, so they ate one more. The next morning, they ate half of the remaining peaches and one more. After that, I ate the remaining half and one every morning. When I wanted to eat again on the 10th morning, I saw that there was only one peach left. Ask how much you picked on the first day.

Program analysis: adopt the method of reverse thinking and infer from back to front.

package com.Pract;

public class LianXi50 {
    public static void fun() {
        int m = 1;
        for (int i = 1; i < 10; i++) {
            m = (m + 1) * 2;
        }
        System.out.println(m);
    }

    public static void main(String[] args) {
        fun();
    }
}

18. Table tennis team

Title: two table tennis teams compete with three players each. Team a consists of a, B and c, and team B consists of x, y and Z. Lots have been drawn to decide the list of matches. The players were asked about the list of matches. A says he doesn't compete with x, c says he doesn't compete with x and Z. please program to find out the list of players of the three teams.

package com.Pract;

public class LianXi50 {
    public static void fun() {
        for (char i = 'x'; i <= 'z'; i++) {
            for (char j = 'x'; j <= 'z'; j++) {
                if (i != j) {
                    for (char k = 'x'; k <= 'z'; k++) {
                        if (i != k && j != k) {
                            if (i != 'x' && k != 'x' && k != 'z') {
                                System.out.println(i + " " + j + " " + k);
                            }
                        }
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        fun();
    }
}

19. Print diamond

Title: enter any odd number and print diamond

Program analysis: first divide the graph into two parts, the first n/2+1 line and the last n/2 line.

package com.Pract;

import java.util.Scanner;

public class LianXi50 {
    public static void fun(int n) {
        int m = n / 2;
        for (int i = 0; i < m + 1; i++) {
            for (int j = 0; j < m - i; j++) {
                System.out.print(" ");
            }
            for (int j = 0; j <= i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < i + 1; j++) {
                System.out.print(" ");
            }
            for (int j = i; j < m; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        fun(new Scanner(System.in).nextInt());
    }
}

20. Sequence summation

Title: there is a score sequence: 2 / 1, 3 / 2, 5 / 3, 8 / 5, 13 / 8, 21 / 13... Find the sum of the first 20 items of this sequence.

Program analysis: please grasp the change law of numerator and denominator.

package com.Pract;

public class LianXi50 {
    public static void fun() {
        double a = 1;
        double b = 2;
        int i = 0;
        double sum = 0;
        while (++i <= 20) {
            sum += 1.0 * (b / a);
            double t = b;
            b = a + b;
            a = t;
        }
        System.out.println(sum);
    }

    public static void main(String[] args) {
        fun();
    }
}

21, 20 factorial summation

Title: ask 1+ 2!+ 3!+…+ 20! Sum of

Program analysis: This program only turns accumulation into cumulative multiplication.

package com.Pract;

public class LianXi50 {
    public static void fun() {
        int sum = 0;
        for (int i = 1; i < 21; i++) {
            int re = 1;
            for (int j = 1; j <= i; j++) {
                re *= j;
            }
            sum += re;
        }
        System.out.println(sum);
    }

    public static void main(String[] args) {
        fun();
    }
}

22. Recursive factorial

Title: using recursive method to find 5!.

Program analysis: recursive formula: return n*fun(n-1).

package com.Pract;

public class LianXi50 {
    public static int fun(int n) {
        if (n == 0)
            return 1;
        else
            return n * fun(n - 1);
    }

    public static void main(String[] args) {
        System.out.println(fun(5));
    }
}

23. Recursive age

Title: five people sat together and asked how old the fifth person was? He said he was two years older than the fourth man. Asked the age of the fourth person, he said that he was 2 years older than the third person. Ask the third person and say that he is two years older than the second. Ask the second person and say that he is two years older than the first. Finally, the first person was asked. He said he was 10 years old. How old is the fifth person?

Program analysis: using the recursive method, recursion is divided into two stages: recursive and recursive. To know the age of the fifth person, you need to know the age of the fourth person, and so on, push it to the first person (10 years old), and then push it back.

package com.Pract;

public class LianXi50 {
    public static int fun(int n) {
        if (n == 1) {
            return 10;
        } else {
            return 2 + fun(n - 1);
        }
    }

    public static void main(String[] args) {
        System.out.println(fun(5));
    }
}

24. Number of digits and reverse order printing

Title: to give a positive integer, requirements: first, find how many digits it is, and second, print out the numbers in reverse order.

package com.Pract;

import java.util.Scanner;

public class LianXi50 {
    public static void fun(int n) {
        int count = 0;
        while (n != 0) {
            System.out.print(n % 10);
            count++;
            n /= 10;
        }
        System.out.println("\n" + count);
    }

    public static void main(String[] args) {
        fun(new Scanner(System.in).nextInt());
    }
}

25. Number of palindromes

Title: judge whether a number is a palindrome number. That is, 12321 is the palindrome number, the number of bits is the same as 10000 bits, and the number of tens is the same as thousands.

Program analysis: judge that the first character is the same as the last character, the second character is the same as the penultimate character, and so on.

package com.Pract;

import java.util.Scanner;

public class LianXi50 {
    public static void fun(String str) {
        char[] c = str.toCharArray();
        int flag = 1;
        for (int i = 0; i < c.length / 2; i++) {
            if (c[i] != c[c.length - 1 - i]) {
                flag = 0;
                System.out.println("NO");
                break;
            }
        }
        if (flag == 1)
            System.out.println("YES");
    }

    public static void main(String[] args) {
        fun(new Scanner(System.in).nextLine());
    }
}

26. Sorting

Title: sort the 10 numbers entered.

Program analysis: bubble sorting method.

package com.Pract;

import java.util.Scanner;

public class LianXi50 {
    public static void fun(int[] a) {
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a.length - 1 - i; j++) {
                if (a[j] > a[j + 1]) {
                    int t = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = t;
                }
            }
        }
        for (int i : a) {
            System.out.print(i + " ");
        }
    }

    public static void main(String[] args) {
        int[] a = new int[10];
        for (int i = 0; i < 10; i++) {
            a[i] = new Scanner(System.in).nextInt();
        }
        fun(a);
    }
}

27. Find the sum of diagonal elements of an n*n matrix

Title: find the sum of diagonal elements of an n*n matrix

Program analysis: double for loop control is used to input two-dimensional array, and then a[i][j] is accumulated and output.

package com.Pract;

import java.util.Scanner;

public class LianXi50 {
    public static void fun(int[][] a) {
        int sum = 0;
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a.length; j++) {
                if (i == j || j == a.length - 1 - i) {
                    sum += a[i][j];
                }
            }
        }
        System.out.println(sum);
    }


    public static void main(String[] args) {
        int n = new Scanner(System.in).nextInt();
        int[][] a = new int[n][n];
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a.length; j++) {
                a[i][j] = j + 1;
            }
        }
        fun(a);
    }
}

28. Insert the specified value into the sorted array

Title: there is an ordered array. Now enter a number and ask to insert it into the array according to the original law.

Program analysis: first judge whether this number is greater than the last number, and then consider inserting the number in the middle. After inserting, the number after this element will move back one position in turn.

package com.Pract;

import java.util.Scanner;

public class LianXi50 {
    public static void fun(int[] a, int n) {
        int re = 0;
        for (int i = a.length - 1; i >= 0; i--) {
            if (n > a[i]) {
                re = i;
                break;
            }
        }
        int[] b = new int[a.length + 1];
        b[re + 1] = n;
        for (int i = 0; i < re + 1; i++) {
            b[i] = a[i];
        }
        for (int i = a.length - 1; i > re; i--) {
            b[i + 1] = a[i];
        }
        for (int i : b) {
            System.out.print(i + " ");
        }
    }

    public static void main(String[] args) {
        int n = new Scanner(System.in).nextInt();
        int[] a = {1, 2, 3, 4, 5, 6, 7};
        fun(a, n);
    }
}

29. Yang Hui triangle

Title: print out n lines of Yang Hui triangle

Program analysis: use a two-dimensional array to store each row of data. The first and last items of each row are 1, and each other item is equal to the item position j of the previous row of the row plus the item position j-1.

 com.Pract;

import java.util.Scanner;

public class LianXi50 {
    public static void fun(int n) {
        int triangle[][] = new int[n][];
        for (int i = 0; i < n; i++) {
            triangle[i] = new int[i + 1];
            for (int j = 0; j < n - 1 - i; j++) {
                System.out.print(" ");
            }
            for (int j = 0; j < triangle[i].length; j++) {
                if (i == 0 || j == 0 || j == triangle[i].length - 1) {
                    triangle[i][j] = 1;
                } else {
                    triangle[i][j] = triangle[i - 1][j] + triangle[i - 1][j - 1];
                }
                System.out.print(triangle[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        int n = new Scanner(System.in).nextInt();
        fun(n);
    }
}

30. Maximum and minimum element exchange

Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.

package com.Pract;

public class LianXi50 {
    public static void fun(int[] a) {
        for (int i = 0; i < a.length; i++) {
            if (a[i] > a[0]) {
                int t = a[i];
                a[i] = a[0];
                a[0] = t;
            }
        }
        for (int i = 0; i < a.length; i++) {
            if (a[a.length - 1] > a[i]) {
                int t = a[i];
                a[i] = a[a.length - 1];
                a[a.length - 1] = t;
            }
        }
        for (int i : a) {
            System.out.print(i + " ");
        }
    }

    public static void main(String[] args) {
        int[] a = {10, 1, 2, 8, 4, 5, 6, 7};
        fun(a);
    }
}

31. Number of mobile m units

Title: there are n integers, so that each number in front of it moves backward m positions, and the last m numbers become the first m numbers.

package com.Pract;

import java.util.Scanner;

public class LianXi50 {
    public static void fun(int[] a, int m) {
        int[] b = new int[a.length];
        for (int i = 0; i < m; i++) {
            b[i] = a[a.length - m + i];
        }
        for (int i = 0; i < a.length - m; i++) {
            b[m + i] = a[i];
        }

        for (int i : b) {
            System.out.print(i + " ");
        }
    }

    public static void main(String[] args) {
        int n = new Scanner(System.in).nextInt();
        int[] a = new int[n];
        int m = new Scanner(System.in).nextInt();
        for (int i = 0; i < n; i++) {
            a[i] = i;
        }
        fun(a, m);
    }
}

32. Counting

Title: there are n people in a circle and number them in sequence. Start counting from the first person (counting from 1 to 3). Those who report to 3 quit the circle and ask what number they left last.

package com.Pract;

import java.util.Scanner;

public class LianXi50 {
    public static void fun(int[] a,int m) {
        int k = 0;
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < m; j++) {
                if (k == a.length) {
                    k = 0;
                }
                if (a[k] == 1) {
                    j--;
                }
                k++;
            }
            k--;
            a[k] = 1;
        }
        System.out.println(k + 1);
    }

    public static void main(String[] args) {
        int n = new Scanner(System.in).nextInt();
        int[] a = new int[n];
        int m = new Scanner(System.in).nextInt();
        for (int i = 0; i < n; i++) {
            a[i] = 0;
        }
        fun(a,m);
    }
}

33. String sorting

Title: string sorting. (dictionary order)

package com.Pract;

public class LianXi50 {
    public static void fun(String[] str) {
        for (int i = 1; i < str.length; i++) {
            for (int j = 0; j < str.length - i; j++) {
                if (str[j].compareTo(str[j + 1]) > 0) {
                    String strings = str[i];
                    str[i] = str[i + 1];
                    str[i + 1] = strings;
                }
            }
        }
        for (String s : str) {
            System.out.print(s + " ");
        }
    }

    public static void main(String[] args) {
        String[] str = {"789", "wer", "123", "zxcxvcv", "456", "sddff"};
        fun(str);
    }
}

34. Monkey Split peach

Title: there is a pile of peaches on the beach, which are divided by five monkeys. The first monkey divided the pile of peaches into five, one more. The monkey threw the one more into the sea and took one more. The second monkey divided the remaining peaches into five parts on average, and one more. It also threw one more into the sea and took one. The third, fourth and fifth monkeys did so. How many peaches were there on the beach?

package com.Pract;

import java.util.Scanner;

public class LianXi50 {
    public static void fun(int n) {
        int res = 1;// The total number of peaches used to control the increase of cyclic variables
        int left = 1;// Number of peaches left
        int count = 0;// Number of monkeys that can be divided
        while (true) {
            System.out.println(left);
            if ((left - 1) % 5 == 0) {    // You can divide another monkey
                ++count;
                left = (left - 1) / 5 * 4;
            } else if (count != n) {    // Loop variable plus one
                left = ++res;
                count = 0;
            }
            if (count == n) {    // Meet the total number of monkeys, okay, I found it
                System.out.println(res);
                break;
            }
        }
    }

    public static void main(String[] args) {
        fun(new Scanner(System.in).nextInt());
    }
}

35. Find the odd number that can be composed of 0-7

Title: find the odd number that can be composed of 0-7.

Program analysis: the number of single digits is only 1, 3, 5 and 7. Except for the first item, there are 7 choices for other items, and there are 6 choices for the first item.

package com.Pract;

public class LianXi50 {
    public static void fun() {
        int sum = 0;
        int g = 4;
        int s = 6;
        int o = 7;
        //Single digit
        sum += g;
        //Double digit
        sum += s * g;
        //Three digit
        sum += s * o * g;
        //Four digit
        sum += s * o * o * g;
        //Five digits
        sum += s * o * o * o * g;
        //Six digit
        sum += s * o * o * o * o * g;
        //Seven digits
        sum += s * o * o * o * o * o * g;
        System.out.println(sum);
    }

    public static void main(String[] args) {
        fun();
    }
}

36. Even numbers are expressed as the sum of two prime numbers

Title: an even number can always be expressed as the sum of two prime numbers.

Program analysis: an even number must be entered. 1 is not a prime number, and 2 cannot be expressed as the sum of two prime numbers.

package com.Pract;

import java.util.Scanner;

public class LianXi50 {
    public static boolean isPrime(int n) {
        if (n == 1)
            return false;
        for (int i = 2; i < (int) Math.sqrt(n) + 1; i++) {
            if (n % i == 0)
                return false;
        }
        return true;
    }

    public static void fun(int n) {
        for (int i = 2; i < n; i++) {
            if (isPrime(i) && isPrime(n - i)) {
                System.out.println(i + "+" + (n - i) + "=" + n);
                break;
            }
        }
    }

    public static void main(String[] args) {
        fun(new Scanner(System.in).nextInt());
    }
}

37. Company data encryption

Title: a company uses a public telephone to transmit data. The data is a four digit integer and is encrypted in the transmission process. The encryption rules are as follows: add 5 to each number, then replace the number with the remainder of sum divided by 10, and then exchange the first and fourth bits, and the second and third bits.

Program analysis: output array in reverse order.

package com.Pract;

import java.util.Scanner;

public class LianXi50 {
    public static void fun(int n) {
        int[] a = new int[4];
        int i = 0;
        while (n != 0) {
            a[i] = n % 10 + 5;
            n /= 10;
            i++;
        }
        for (int j = 0; j < 4; j++) {
            System.out.print(a[j]);
        }
    }

    public static void main(String[] args) {
        fun(new Scanner(System.in).nextInt());
    }
}

38. Calculate the number of occurrences of substrings in the string

Title: calculate the number of substrings in a string, that is, the number of strings separated by spaces.

Program analysis: use string segmentation function to separate spaces.

package com.Pract;

import java.util.Scanner;

public class LianXi50 {
    public static void fun(String string) {
        String[] str = string.split(" ");
        System.out.println(str.length);
    }

    public static void main(String[] args) {
        fun(new Scanner(System.in).nextLine());
    }
}

39. Find the specified element by dichotomy

Title: give a sorted array and use dichotomy to find elements.

package com.Pract;

public class LianXi50 {
    public static int erFen(int[] a, int begin, int end, int target) {
        int mid = (begin + end) / 2;
        if (begin > end) {
            return 0;
        }
        if (target < a[mid])
            return erFen(a, begin, mid, target);
        else if (target > a[mid])
            return erFen(a, mid, end, target);
        else
            return mid;
    }

    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        System.out.println(erFen(a, 0, a.length, 5));
    }
}

Keywords: Java Eclipse JavaSE intellij-idea

Added by celebx on Sun, 16 Jan 2022 06:20:53 +0200