【Daily Programming Day29】There are counterfeit coins

Table of contents

1. Multiple choice questions

2. Programming questions

1. There are counterfeit money


1. Multiple choice questions

Focus on reviewing multiple-choice questions 4, 8, and 10.

Question 4:

Class method: also known as static method. The instance method cannot add static, also called non-static method.

Difference between class method and instance method_Difference between class method and instance method

(1) A error, the instance method of the class is associated with the instance object of the class, and cannot be called directly, but can only be called by creating an instance object of the superclass.
(2) B error, when the class method of the parent class is defined as private, it is invisible to the subclass, so the subclass cannot call it.
(3) C error. The specific instance method of the subclass is invisible to the parent class, so it cannot be called directly. It can only be called by creating an instance object of the subclass.
(4) D is correct, an instance method can call an instance method in its own class.


Question 10:

Test points: [JavaSE] About the difference between the direct assignment method and the new method to generate objects?

        Direct assignment method: (private String name="abc"): first check if there is such a String object in the constant pool, if not, create one, and if so, directly get a reference to it; (1) The name is not modified by static, so every time an object is instantiated, private String name="abc" will be executed. When it is executed for the first time, it is found that the string constant pool does not have a String object whose value array is ['a', 'b', 'c'], so create one, so the result is false
;

(2) But the second time I found that there is already such an object, so I just got a reference to this object, so the result of 2 returns true.
(3) When executing test.name == testB.name, the comparison is whether the memory pointed to by the two names is the same (comparing the reference itself is meaningless), so the result of test.name==testB.name is also true.

reference blog

 access permission:


2. Programming questions

1. There are counterfeit money

 

 

 

 【Thinking】

It is known that the counterfeit currency is lighter than the real currency, and the request is the fastest. How many times do you need to ask at most?

1. The fastest requirement: so how many groups should be considered the most appropriate?

In theory, it must be that the fewer the number of copies, the faster it is, because the more times, the more time it takes. Then, comparing two piles with three piles, if divided into two piles, only half of the probability can be eliminated after each weighing, but 2/3 of the probability can be eliminated by dividing into three piles, which is more efficient.

2. According to the number of coins, there are mainly the following situations:

(1) If there is only one coin: it must be a counterfeit coin, and the number of comparisons is 0;

(2) If there are three coins (including three): the number of comparisons is 1;

(3) If the number of coins is greater than three:

        If n%3==0, the number of coins can be divided equally, and the largest of the three is taken, n=n/3;

If it cannot be divided equally, there will only be one or two extra coins at this time. Although the largest one is taken, the extra one or two coins need to be added, but because it is the fastest, we try to meet the condition of "equal distribution" and divide the coins evenly, so if there is one more, the number of the largest share at this time is n/3+1. 1, and the other is n/3. Therefore, the most shares are n/3+1.

(For example: 11 points, divided into 4,4,3 instead of 3,3,5. n = 11/3+1)

        Therefore, in this way, two of the three shares must be the same, and each time the same two shares are compared. If they are equal, it means that the counterfeit currency is in the third share. If not, then the counterfeit currency is in the lightest of the two shares.

Reference Blog 1

Reference Blog 2

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            int n = sc.nextInt();
            //如果输入数字0,表示结束
            if(n == 0){
                break;
            }
            //如果只有一张,则一定是假币
            if(n == 1){
                System.out.println(0);
            } //如果有三张以下的钱,则最多称重一次
            else if (n <= 3) {
                System.out.println(1);
            }  //当假币数量大于三时,进行三等分:
            else if (n > 3) {
                int count = 1;
                while (n >3){
                    //如果能够等分:n表示等分后每份中的数量
                    if(n % 3 == 0){
                        n/=3;
                    }else{//不能等分:
                        n = n/3+1;
                    }
                    count++;
                }
                System.out.println(count);
            }
        }
    }

Guess you like

Origin blog.csdn.net/qq_48479056/article/details/131402640
Recommended