Teng Xun's Written Test Programming in Autumn 2017

First,
Assuming that the encoding range of a code is 25 letters of a ~ y, encoding from one bit to four bits, if we sort the encoding in dictionary order, we can form an array as follows: a, aa, a aaa, aaab, aaac,... b, ba, baa, baaa, baab, baac... yyyw, yyyx, yyyy where Index of a is 0, Index of AA is 1, Index of AAA is 2, and so on. Write a function, input is any code, output the corresponding Index of this code.
Input Description:
Enter a string to be encoded. The length of the string is less than or equal to 100.
Output description:
Output the index of this encoding
input
baca
output
16331

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        /* Find out the number of strings smaller than str, and use enumeration.
         * Analysis of hijk
         * A string of length 1, smaller than it are a, b,... h, 8 (including itself). Determine res=(ch[0]-'a') +1
         * Strings of length 2, smaller than it are a, aa~ay, b, ba~by,..., h, ha~hi, except len1, there are 25*7+9. Determine res+=25*(ch[0]-'a')+(ch[1]-'a')+1
         * The length of the string is 3, except that the smaller ones are aaa~aay, aba~aby,..., aya~ayy,..., haa~hay,..., hia~hij. There are 25*25*7+25*8+10
         * Determine res+=25*25*(ch[0]-'a')+25*(ch[1]-'a')+ch[2]-'a'+1
         * It can be directly determined that res+=25*25*25(ch[0]-'a')+25*25(ch[1]-'a')+25*(ch[2]-'a')+ch[3]-'a'(excluding hijk)
         * Recursive formulas can be obtained.
         */
        while(sc.hasNext()) {
            String str = sc.next();
            char []ch = str.toCharArray();
            int len = ch.length;
            int res = 0;
            int cur = 0;
            for(int i = 0; i < 4; i++) {
                cur *= 25;
                if(i < len) {
                    cur += (ch[i] - 'a');
                }
                res += cur;
                if(i + 1< len) {
                    res += 1;
                }
            }
            System.out.println(res);
        }
        sc.close();
    }
}

Two.
There are many kinds of tasks in the game, one of which can only be done once by players. There are 1024 such tasks, and the range of task ID is [1,1024]. Please use 32 unsigned int types to record whether 1024 tasks have been completed. The initial state is incomplete. Enter two parameters, both task IDs, the task that needs to set the first ID to be completed; and check whether the task of the second ID has been completed. Output a parameter, if the task of the second ID has completed output 1, if it has not completed output 0. If the first or second ID is not in the range of [1,1024], output-1.
Input Description:
The input consists of one line and two integers representing the persona ID.
Output description:
Whether the output is complete or not
input
1024 1024
output
1

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int []bit = new int[32];//Only 0-1023 can be saved.
        while(sc.hasNext()) {
            int num1 = sc.nextInt();
            int num2 = sc.nextInt();
            if(num1>1023 || num1<0 || num2>1023 || num2<0) {
                System.out.println(-1);
            }else {
                bit[num1/32] = 1 << num1 % 32;
                if ((bit[num2/32] & 1 << num2 % 32) != 0) {
                    System.out.print(1);
                }else { 
                    System.out.print(0);
                }
            }
        }
        sc.close();
    }
}

Three.
Given a positive integer, write a program to calculate how many pairs of prime numbers are equal to the input of the positive integer, and output the results. The input value is less than 1000.
For example, if the input is 10, the program should output a result of 2. (The sum of two pairs of prime numbers is 10, which is (5,5), (3,7).)
Input Description:
The input includes an integer n,(3 < n < 1000)
Output description:
Logarithm of output
input
10
output
2

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        List<Integer> prime = new ArrayList<>();
        prime.add(2);
        for(int i = 3; i < 1000; i++) {
            if(isPrime(i)) {
                prime.add(i);
            }
        }
        int len = prime.size();
        while(sc.hasNext()) {
            int n = sc.nextInt();
            int res = 0;
            int i = 0, j = len - 1;
            while(i <= j) {
                int sum = prime.get(i) + prime.get(j);
                if(sum == n) {
                    res++;
                    j--;
                    i++;
                }else if(sum < n){
                    i++;
                }else {
                    j--;
                }
            }
            System.out.println(res);
        }
        sc.close();
    }

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

Four.
Gehash coding: Gehash is often used to convert two-dimensional longitude and latitude into a string, which is divided into two steps: the first step is the binary coding of longitude and latitude, and the second step is the base32 transcoding.
This problem examines the binary coding of latitude: the algorithm approximates latitude [-90, 90] infinitely by dichotomy (depending on the required accuracy, the accuracy of this problem is 6). Note that in the dichotomy approximation process, only downward integer is used for dichotomy, and the dichotomy median value belongs to the right interval. Examples of the algorithm are as follows: Binary encoding for latitude 80:
1) The interval [-90, 90] is divided into two parts [-90, 0], [0, 90], and becomes a left and right interval. It can be determined that 80 is a right interval, marked as 1.
2) Dividing the right interval [0, 90] of the previous step into [0, 45], [45, 90], we can confirm that 80 is the right interval, marked as 1;
3) Dividing [45,90] into [45,67], [67,90], 80 can be identified as the right interval, marked as 1;
4) Dividing [67,78], [78,90] into two parts for [67,90], 80 can be identified as the right interval, marked as 1;
5) Dividing [78,90] into [78,84], [84,90], 80 can be identified as the left interval, marked as 0;
6) Dividing [78, 84] into [78, 81], [81, 84], 80 can be identified as the left interval, marked as 0;
Input Description:
The input includes an integer n,(-90 < n < 90)
Output description:
Output Binary Coding
Example 1
input
80
output
111100

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()) {
            int n = sc.nextInt();
            StringBuffer sb = new StringBuffer();
            int l = -90, r = 90;
            //It's easy to find this question by dichotomy.
            for(int i = 0; i < 6; i++) {
                int mid = (l+r) / 2;
                if(n < mid) {
                    sb.append(0);
                    r = mid;
                }else {
                    sb.append(1);
                    l = mid;
                }
            }
            System.out.println(sb.toString());
        }
        sc.close();
    }
}

Keywords: encoding less

Added by meshi on Sun, 09 Jun 2019 01:50:12 +0300