The 14th Blue Bridge Cup Competition - the fourth day of real test training

Table of contents

Question 1: Guess the letter

topic description

topic analysis

topic code

Question 2: Strange Fractions

Title description

topic analysis

topic code

Question 3: Divide candies

topic description

topic analysis

 

Question 1: Guess the letter

topic description

The sequence consisting of 19 letters of abcd...s is repeatedly spliced ​​106 times to obtain a string with a length of 2014. Next, delete the first letter (that is, the letter a at the beginning), as well as the third, fifth, and all odd-numbered letters. The new string that obtains carries out the action of deleting odd-numbered position letters again. Go on like this, until there is only one letter left, please write that letter.
The answer is a lowercase letter, please submit the answer through the browser. Do not fill in any redundant content.

topic analysis

string deletion

This code means to delete elements directly on the original string and return this object
str.deleteCharAt(i)

So the following loop is wrong

while (true){
            if (str.length()==1){
                break;
            }
            for (int i =0;i<str.length();i+=2){
                str.deleteCharAt(i);
            }
        }

topic code

method one:

import java.util.Scanner;
// 1: no package required
// 2: The class name must be Main, which cannot be modified

public class Main {
    public static void main(String[] args) {
        String s = "";
        for(int i=1;i<=106;i++) {
            s = s+ "abcdefghijklmnopqrs";
        }
        String t = "";
        while(s.length()!=1) {
            t = "";
            for(int i=1;i<s.length();i=i+2) {
                t = t + s.charAt(i);
            }
            s = t;
        }
        System.out.println(s);
    }
}

Method Two:

public class Main {
    public static void main(String[] args) {
        String string = "abcdefghijklnmopqls";
        StringBuffer str = new StringBuffer("");
        for (int i = 0;i<106;i++){
             str=str.append(string);
        }

        while (true){
            if (str.length()==1){
                break;
            }
            for (int i =0;i<str.length();i+=2){
                str.replace(i,i+1,"w");
            }
            for (int i = 0;i<str.length();i++){
                if (str.charAt(i)=='w'){
                    str.deleteCharAt(i);
                }
            }

        }
        System.out.println(str);

    }
}

Question 2: Strange Fractions

Title description

When he was in elementary school, Xiao Ming often invented new algorithms by himself. Once, the teacher asked the topic:
1/4 times 8/5
Xiao Ming actually spliced ​​the numerators together and the denominators together, the answer is: 18/45 (see Figure 1.png)
The teacher just wanted to criticize him, but on second thought, this answer happened to be correct, what the hell!
For the case where the numerator and denominator are all single digits from 1 to 9, what other formulas can be calculated in this way?
Please write down the number of all different calculations (including the examples in the question).
Obviously, after exchanging the numerator and denominator, for example: 4/1 multiplied by 5/8 is satisfied, which counts as a different calculation.
But for the case where the numerator and denominator are the same, 2/2 multiplied by 3/3 is too many types to be counted!
Note: The answer is an integer (considering symmetry, it must be an even number). Please submit via browser. Don't write superfluous content.

topic analysis

If you encounter division, you can think of whether it can be converted into multiplication

topic code

public class strange fraction {
    public static void main(String[] args) {
        int count = 0;
        String s1 = null;
        String s2 = null;
        int c = 0;
        int d = 0;
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= 9; j++) {
                if (i != j) for (int k = 1; k <= 9; k++) {
                    for (int l = 1; l <= 9; l++) {
                        if (k != l) {
                            s1 = (i + "") + (k + "");
                            s2 = (j + "") + (l + "");
                            c = Integer.parseInt(s1);
                            d = Integer.parseInt(s2);
                            if (i * k * d == j * l * c) {
                                count++;
                            }
                        }
                    }
                }
            }
        }
        System.out.println(count );
    }
}

Question 3: Divide candies

topic description

There are n children sitting in a circle. The teacher randomly distributes an even number of candies to each child, and then plays the following game:
Each child distributes half of his candy to the child on the left.
After a round of candy distribution, the teacher will supply 1 candy to the child with an odd number of candies, thus becoming an even number.
Repeat this game until all children have the same number of candies.
Your task is to predict how many candies the teacher will need to reissue under the known initial candy situation.
[format requirement]
The program first reads in an integer N (2<N<100), indicating the number of children.
Then there is a line of N even numbers separated by spaces (each even number is not greater than 1000 and not less than 2)
The program is required to output an integer representing the number of candies that the teacher needs to reissue.
For example: enter
3
2 2 4
The program should output:
4

topic analysis

A circle can be represented by an array, and the first person and the last person are treated specially

topic code

import java.util.Scanner;

public class Divide candy {
    public static void main(String[] args) {
        Scanner sca = new Scanner(System.in);
        int n = sca.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = sca.nextInt();
        }
        int tangGuo = 0;
        while (true) {
            //odd number plus candy
            for (int i = 0; i < arr.length; i++) {
                if (arr[i] % 2 != 0) {
                    arr[i]++;
                    tangGuo++;
                }
            }
            //Divide candy
            int temp = arr[0] / 2;
            for (int i = 0; i < arr.length - 1; i++) {
                arr[i] = arr[i] / 2 + arr[i + 1] / 2;
            }
            arr[arr.length - 1] = temp + arr[arr.length - 1] / 2;
            //Record the number of people with the same candy
            int x = 0;
            for (int i = 0; i <= arr.length - 2; i++) {
                if (arr[i] == arr[i + 1]) {
                    x++;
                }
            }
            //Note here is n-1
            if (x==n-1){
                System.out.println(tangGuo);
                break;
            }

        }
    }
}

Tags: Java Algorithm

Posted by nuklehed on Sun, 19 Mar 2023 03:32:18 +0530