Reflections on Huawei's Written Test of September 7, 2019

Topic 1

Enter an array

[ 8,7,1,2,3,4,5,2,2,5,6];

The first number can be taken [1-len/2], and then the number can be used as subscript a.

The next number is the value of a, arr[a], which is the number of steps, and arr[a] step backwards.

Find the minimum number of steps to reach the end

The answer to the above array is: 3

8 -> 7 ->2 ->6

 

Answer:

  public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        String[] c = s.split(" ");
        int res = s.length();
        int tmp = -1;
        if (c.length==1){
            System.out.println(1);
        }
        else {
            for (int i=1;i<c.length/2;i++){
                int j=i;
                int count =1;
                while (j<c.length-1){
                    j=j+(Integer.parseInt(c[j]));
                    count++;
                }
                if (j==c.length-1){
                    tmp =count;
                    res = Math.min(res,tmp);
                }
            }
            if (res==s.length()) System.out.println(-1);
            else System.out.println(res);
        }
    }

Question 2

Topic Description
In dota games, summoners can control the three elements of ice, thunder and fire, and generate new skills through meta-table combination. Now we have revised a new map in which he can control n elements and form a new skill with m elements in a circle (the m elements are counted as repetition by rotation or inversion, such as 123, 231, 312, 321, 213 and 132), so how many skills can the summoner group?
(2000 > = n > = 1, 1 < = m < = 10000), because the result may be very large, please take the balance of the result to 00000007.

Input Description:
n and m


Output description:
Number of Combination Skills

 

Such as:

Input: 3,3

Output: [[1, 1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 2], [1, 2, 3], [1, 3, 3], [2, 2], [2, 2], [2, 2, 3], [2, 3, 3], [3, 3]]

 

Answer: No, no solution has been found yet.

package IMUHERO;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class Main {
    static LinkedList<List<Integer>>res = new LinkedList<>();
    static int count=0;
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int k = scanner.nextInt();
        if (n==0||k==0) System.out.println(0);
        else {
            LinkedList<Integer>list = new LinkedList<>();
            Combine(n,k,1,list);
            System.out.println(res);
            System.out.println(count);
        }
    }
    private static void Combine(int n,int k,int start,LinkedList<Integer>list){
        if (list.size()==k){
            res.addLast((LinkedList<Integer>)list.clone());
            count = (count+1)%1000000007;
            return;
        }

        for (int i=start;i<=n;i++){
            list.addLast(i);
            Combine(n,k,i,list);
            list.removeLast();
        }
        return;
    }
}




 

Question 3

Topic description

In the communication industry, indicators are often used to record the business data of a certain area, such as the indicators of the number of handover requests of cell users and the number of successful handover requests of cell users. Indicators can be divided into basic indicators and complex indicators according to their types. Among them, the complex index is calculated from the basic index.

For example, the success rate of cell user handover is a complex index, and the formula is: the success rate of cell user handover = the number of successful cell user handover requests / the number of cell user handover* 100; now give each index a HD-only description instead of the Chinese description, and use brackets [enclosed, for the constants in the formula]. Brackets 0 enclosed;

If the cell user handover success rate index ID-1123654], the cell user handover request success rate index 1D-|6598], and the cell user handover number index D-(6665), the above cell user handover success rate formula can be expressed as: [123654]-16598] 6665] (100).

Requirements: Input the calculation formula of complex index and the value of basic index contained in the formula, calculate the value of specified index and return it;

Input

[1234]=[12]+[34]*{50},[12]=[1]+[2]/{2};[1]=10,[2]=20,[34]=50;[1234]

Output:

[1234] calculation results: 2520

package IMUHERO;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Scanner;
//[1234]=[12]+[34]*{50},[12]=[1]+[2]/{2};[1]=10,[2]=20,[34]=50;[1234]
public class Main1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        String [] sArr = s.split(";");
        String [] GS = sArr[0].split(",");
        String [] KV = sArr[1].split(",");
        String [] Value = sArr[2].split("");

        HashMap<String,String> map = new HashMap();
        for (int i=0;i<KV.length;i++){
            String [] keyValue = KV[i].split("=");
            String key = keyValue[0].substring(1,keyValue[0].length()-1);
            String value = keyValue[1];
            map.put(key,value);
        }

        for (int i=GS.length-1;i>=0;i--){
            String [] keyValue = GS[i].split("=");
            String key = keyValue[0].substring(1,keyValue[0].length()-1);
            LinkedList<String>list = new LinkedList<>();
            String val = keyValue[1];
            for (int m=0;m<val.length();m++){
                if (val.charAt(m)=='['){
                    int n=m;
                    while (val.charAt(n)!=']')n++;
                    String Skey = val.substring(m+1,n);
                    list.addLast(map.get(Skey));
                    m=n;
                }
                else if (val.charAt(m)=='{'){
                    int n=m;
                    while (val.charAt(n)!='}')n++;
                    list.addLast(val.substring(m+1,n));
                    m=n;
                }
                else list.addLast(String.valueOf(val.charAt(m)));
            }
            while (!list.isEmpty()){
                if (list.size()==1){
                    map.put(key,list.removeFirst());
                    break;
                }
                int one = Integer.parseInt(list.removeFirst());
                String sign1 = list.removeFirst();
                int two = Integer.parseInt(list.removeFirst());
                if (sign1.equals("+")||sign1.equals("-")) {
                    if (!list.isEmpty()){   //Judgment +, - followed by *,./
                        String sign2 = list.getFirst();
                        while (!list.isEmpty()&&sign2.equals("/")||sign2.equals("*")){
                            sign2 = list.removeFirst();
                            int three = Integer.parseInt(list.removeFirst());
                            if (sign2.equals("/")){
                                list.addFirst(String.valueOf(two/three));
                            }
                            else {
                                list.addFirst(String.valueOf(two*three));
                            }
                            two = Integer.parseInt(list.removeFirst());
                            if (!list.isEmpty())sign2 = list.getFirst();
                            else sign2="empty";     //Indicates that the list has been computed and jumps out
                        }
                    }
                    if (sign1.equals("+")){
                        list.addFirst(String.valueOf(one+two));
                    }
                    else {
                        list.addFirst(String.valueOf(one-two));
                    }
                }
                else {
                    if (sign1.equals("/")){
                        list.addFirst(String.valueOf(one/two));
                    }
                    else {
                        list.addFirst(String.valueOf(one/two));
                    }
                }
            }
        }
        StringBuffer stringBuffer = new StringBuffer();
        for (String c:Value){
            if (!c.equals("[")&&!c.equals("]")){
                stringBuffer.append(c);
            }
        }
//        System.out.println(stringBuffer);
        System.out.println(map.get(stringBuffer.toString()));

    }
}


 

Tags: Java

Posted on Mon, 09 Sep 2019 21:36:26 -0700 by wiggst3r