[JavaSE Series] Basic Classical Programming Questions

First, determine whether it is a prime number 

Question: Given a number, determine whether a number is prime.

Prime Number: Among the natural numbers greater than 1, a natural number that has no other factors than 1 and itself .

For example, 2, 3, 5, 7, 11, 13, ... are prime numbers; 4, 8, 9, 10, 12, 14, ... are not prime numbers.

Method 1: Solving by Violence

Code example:

import java.util.Scanner;

public class TestDemo_4 {
    /**
     * 判断是不是素数的方法
     * @param num -> num是所要判断的数字
     * @return false 说明不是素数;true说明是素数
     */
    public static boolean isPrime(int num){
        for (int i = 2; i < num; i++) {
            if (num % i == 0){
                return false;
            }
        }
        return true;  //代码如果可以执行到这一行,说明前面的不满足条件,即到这一行的话 num是素数
    }

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

        if (isPrime(num)){
            System.out.println(num+"是素数");
        }else{
            System.out.println(num+"不是素数");
        }
    }
}

Output result:

Method 2: num/2:

For any number, it can be written as a product: num = a*b;

For example, num = 16 can be written as: 1*16 2*8 3*6 4*4 6*3 8*2 16*1 .

Then we will find that one of the two numbers must be <=num/2.

Therefore, we can optimize the above code, do not need to judge between 2~(num-1), just judge between 2~(num/2), so that the interval to be compared will be reduced by half.

Code example:

import java.util.Scanner;

public class TestDemo_4 {
    /**
     * 判断是不是素数的方法
     * @param num -> num是所要判断的数字
     * @return false 说明不是素数;true说明是素数
     */
    public static boolean isPrime(int num){
        for (int i = 2; i <= num/2; i++) {
            if (num % i == 0){
                return false;
            }
        }
        return true;  //代码如果可以执行到这一行,说明前面的不满足条件,即到这一行的话 num是素数
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();

        if (isPrime(num)){
            System.out.println(num+"是素数");
        }else{
            System.out.println(num+"不是素数");
        }
    }
}

Output result:

Method 3: open the root number of num:

At the same time, we also found that we can also reduce the comparison interval: num can be divided into two numbers to multiply,

We can find that at least one of the two numbers is smaller than the root num,

For example, one of the two numbers multiplied by 16 must be less than 4,

Then, we can reduce the comparison interval again.

At this point, you need to use the Main method:

Code example:

import java.util.*;

public class TestDemo_4 {
    /**
     * 判断是不是素数的方法
     * @param num -> num是所要判断的数字
     * @return false 说明不是素数;true说明是素数
     */
    public static boolean isPrime(int num){
        for (int i = 2; i <= Math.sqrt(num); i++) {
            if (num % i == 0){
                return false;
            }
        }
        return true;  //代码如果可以执行到这一行,说明前面的不满足条件,即到这一行的话 num是素数
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();

        if (isPrime(num)){
            System.out.println(num+"是素数");
        }else{
            System.out.println(num+"不是素数");
        }
    }
}

 Output result:


2. Print prime numbers

Question: Print all prime numbers between 1 and 100.

 Ideas:

The first step: write a judgment method for whether it is a prime number (that is, the above);

Step 2: Filter between 1 and 100, and print out the ones that conform to this method.

Code example:

import java.util.*;

public class TestDemo_4 {
    /**
     * 判断某个数字是不是素数的方法
     * @param num -> num是所要判断的数字
     * @return false 说明不是素数;true说明是素数
     */
    public static boolean isPrime(int num){
        for (int i = 2; i <= Math.sqrt(num); i++) {
            if (num % i == 0){
                return false;
            }
        }
        return true;  //代码如果可以执行到这一行,说明前面的不满足条件,即到这一行的话 num是素数
    }


    public static void main(String[] args) {
        for (int i = 2; i < 100; i++) {     
            if (isPrime(i)){
                System.out.print(i+" ");
            }
        }
    }
}

 Output result:


3. Output leap year

Question: Print all leap years between 1000 - 2000.

leap year:

Ordinary leap year: The Gregorian calendar year is a multiple of 4, and not a multiple of 100, which is a leap year (such as 2004, 2020, etc. are leap years).

Century leap year: The Gregorian calendar year is a whole hundred, and it must be a multiple of 400 to be a leap year (for example, 1900 is not a leap year, and 2000 is a leap year).

Ideas:

Step 1: Write a method for judging whether it is a leap year

Step 2: Filter between 1000 and 2000, and print out the ones that conform to this method.

Code example:

import java.util.*;

public class TestDemo_4 {
    /**
     * 判断传入的数据是不是闰年的方法
     * @param year 输入参数是year
     * @return 是闰年返回true,不是返回false
     */
    public static boolean isLeapYear(int year){
        if (year % 100 != 0 && year % 4 == 0 || year % 400 ==0){
            return true;
        }
        return false;
    }


    public static void main(String[] args) {
        for (int year = 1000; year <= 2000; year++) {
            if (isLeapYear(year)){
                System.out.print(year+" ");
            }
        }
    }
}

Output result:


4. Output multiplication formula table

Output a 9*9 multiplication table.

 Code example:

import java.util.*;

public class TestDemo_4 {
    public static void func(){
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j+"*"+i+"="+i*j+" ");
            }
            System.out.println();
        }
    }

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

Output result:

 


5. Age printing

Question: According to the entered age, print out the current age of people who are teenagers (below 18), youth (19-28), middle-aged (29-55), and old (above 56).

Code example: 

import java.util.*;

public class TestDemo_4 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int age = scanner.nextInt();

        if (0<=age && age<=18){
            System.out.println("少年");
        }else if(19<=age && age<=28){
            System.out.println("青年");
        }else if (29<=age && age<=55){
            System.out.println("中年");
        }else{
            System.out.println("老年");
        }
    }
}

Output result:


       At the end, I need to explain a little. There are several solutions for each problem. The above are just some of the solutions. If you are interested, you can try other solutions! ! ! ! ! !

Attached a favorite picture:

Guess you like

Origin blog.csdn.net/qq_53362595/article/details/123601277