Past programming questions brushing record (most of them with answers)

Perfect World 2017 Fall Recruitment Programming Questions

(2020-09-02 Rewriting record)

Find the smallest number of k in the array in order

For an unordered array, the elements in the array are different integers. Please return the smallest k numbers in the array. The order is the same as the order of the elements in the original array.

Enter a description:

The first line is the length of the array n, the number k to be returned, n >= k The
next n lines are n elements of the array, each line is an integer

Output description:

The output is k lines, each line is an integer

Example:

enter

4 2
1
2
3
4

Output

1
2

time limit

C/C++ language: 100MS other languages: 2100MS

Memory limit

C/C++ language: 10KB Other languages: 524298KB

My answer

import java.util.Scanner;
import java.util.Arrays;

public class Main
{
    
    
    public static void main(String[] args)
    {
    
    
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();
        int[] arr = new int[n];
        int[] a = new int[n];
        
        for(int i = 0 ; i < n ; i++)
        {
    
    
            arr[i] = scanner.nextInt();
            a[i] = arr[i];
        }
        
        Arrays.sort(a);
        int t = a[k - 1];
        int count = 0;
        
        for(int i = 0 ; i < n ; i++)
        {
    
    
            if(arr[i] <= t && count <= k)
            {
    
    
                System.out.println(arr[i]);
                count++;
            }
            else if(count > k)
            {
    
    
                break;
            }
        }
    }
}

Bilibili 2019 autumn recruit technical post programming questions

(2020-08-14 Rewriting record)

Given an integer array, judge whether there are 3 numbers and the sum is N

Enter a description:

The input is a line.
Before the comma is an integer array, each element is separated by a space; after the comma is N

Output description:

The output bool value
True means there are 3 numbers with a sum of N
False means there are no 3 numbers with a sum of N

Example:

enter

1 2 3 4 5,10

Output

True

Remarks:

The length of the array does not exceed 2000, so the numbers are all positive integers in the int range

My answer

import java.util.Scanner;

public class Main
{
    
    
    public static void main(String[] args)
    {
    
    
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        String[] input = s.split(",");
        String[] arr = input[0].split(" ");
        int N = Integer.parseInt(input[1]);
        int i, j, k, a, b, c;
        
        for(i = 0 ; i < arr.length - 2 ; i++)
        {
    
    
            a = Integer.parseInt(arr[i]);
            if(a >= N) continue;
            for(j = i + 1 ; j < arr.length - 1 ; j++)
            {
    
    
                b = Integer.parseInt(arr[j]);
                if(a + b >= N) continue;
                for(k = j + 1 ; k < arr.length ; k++)
                {
    
    
                    c = Integer.parseInt(arr[k]);
                    if(a + b + c == N)
                    {
    
    
                        System.out.println("True");
                        return;
                    }
                }
            }
        }
        System.out.println("False");
    }
}

Capsule machine

Niang 22 and Niang 33 received the task of capsule toy from Little TV:
There are two capsule machines, numbered as capsule machine No. 2 and capsule machine No. 3. Niang 22 uses capsule machine No. 2 and Niang 33 uses capsule toy. Capsule machine No. 3.
Coin capsule machines do not need, but a special ability:
capsule machines No. 2: If the plug x (x ranges from> 0 = positive integer) Mini capsule toy in, can then be twisted to 2x + 1 th
Mini capsule toy Machine No. 3: If you plug x (the range of x is >=0 positive integer) capsules in, then you can twist to 2x+2
22 Niangs and 33 Niangs without any capsules in their hands. You need to help them design a plan. People "twist in turns" (the one who starts first is not limited, and the twisted eggs can be handed over to the other party), using the "minimum" number of times, so that they can twist exactly as many as N and give it to the little TV.

Enter a description:

Enter a positive integer to represent the N gashapons that Little TV Master needs.

Output description:

Output a string, each character represents the gashapon machine, the characters can only contain "2" and "3".

Example:

enter

10

Output

233

Remarks:

1<=N<=1e9

My answer

import java.util.Scanner;

public class Main
{
    
    
	public static void main(String[] args)
    {
    
    
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        String s = "";
        
        while(N != 0)
        {
    
    
            if(N % 2 == 0)
            {
    
    
                N = (N - 2) / 2;
                s = "3" + s;
            }
            else
            {
    
    
                N = (N - 1) / 2;
                s = "2" + s;
            }
        }
        System.out.println(s);
    }
}

Microsoft China 2021 school recruitment written test questions

(Memories + part of the answer included)

Missing Bowl

There are N bowls arranged on a table in a row. Each bowl contains some marbles in such a way that the sum of the number of marbles in the first and the last bowl is equal to the sum of the number of marbles in the second and the second last bowl, and so forth. The bowls are kept in the increasing order of the number of marbles in it.

After some time, It is found that there are only N-1 bowls. The first and the last bowls are at their positions, but one of the bowls in between has gone missing.

Find the number of marbles in the missing bowl, given that N%2==0.

Input Specification:

input1:

N-1, number of bowls

input2:

Array of size N-1, storing number of marbles in each bowl.

Output Specification:

Your function should return the number of marbles in the missing bowl.

Example 1:

input1:

5

input2:

{1,3,5,9,11}

Output

7

Explanation:

The total sum of marbles in two bowls should be 12. Hence, the missing bowl is Bowl 4 which must contain 7 marbles.

Example 2:

input1:

5

input2:

{2,4,6,10,12}

Output

8

===================================================

Love Letter

You write a love letter to your friend. However, before your friend can read it, someone else takes it and rotates the characters of each word left to right K times. Find the number of words that remain the same even after this shifting of letters.

Note: There can be more than one spaces between the words.

Input Specification:

input1:

String of words

input2:

K, number of times rotation happens

Output Specification:

Your function should return the number of correct words.

Example 1:

input1:

llohe ereth

input2:

2

Output

0

Explanation:

In example 1, “llohe ereth” is a rotated string. Hence, the original string was “hello there” which is not correct. Hence answer is 0.

Example 2:

input1:

you

input2:

3

Output

1

Explanation:

In example 2, “adaada” when rotated 3 times, gives back “adaada”. Hence answer is 1.

My answer

import java.util.Scanner;

public class Main
{
    
    
	private static String rotate(String word,int K)
	{
    
    
		String str1 = word.substring(0, K);
		String str2 = word.substring(K);
		return str2 + str1;
	}
	
	public static void main(String[] args)
    {
    
    
        Scanner scanner = new Scanner(System.in);
        String input1 = scanner.nextLine();
        if(input1.trim().equals(""))
        {
    
    
        	 System.out.println(0);
        	 return;
        }
        int input2 = scanner.nextInt();
        int count = 0;
		String[] words = input1.trim().split("\\s+");
		for(int i = 0 ; i < words.length ; i++)
		{
    
    
			String cword = words[i].trim();
			int r = input2 % cword.length();
			if(r == 0)
			{
    
    
				count++;
			}
			else if(cword.equals(rotate(cword, r)))
			{
    
    
				count++;
			}
		}
		System.out.println(count);
	}
}

===================================================

The Ultimate Fight

Piccolo Junior is the exact same copy of his father, King Piccolo and that means they know each other’s moves and attacking strengths and are equally powerful. Piccolo Junior will use equal number of attacks and to respect his father, he will never perform an extra attack before his father.

But the actual attack did not occur this way and Goku visited the past and made changes in the attack sequence i.e. replaced Piccolo Jr.'s attack with King Piccolo’s and vice versa wherever required to maintain the original constraint.

How many minimum number of replacements are required to do so?

Input Specification:

input1:

Sequence containing ‘K’ for king and ‘J’ for junior.

Output Specification:

Return minimum number of replacements required, if this cannot be done, return -1

Example 1:

input1:

JK

Output

2

Explanation:

Since king has to attack first, J should be changed to K and to make equal number of attacks, K should be replaced with J.

Example 2:

input1:

KKKK

Output

2

Explanation:

Second and fourth moves should be changed to J moves.

Guess you like

Origin blog.csdn.net/qq_39517716/article/details/108368079
Recommended