过往编程题刷题记录(大部分附答案)

完美世界2017秋招编程题

(2020-09-02 刷题记录)

按序找到数组中最小的k个数

对于一个无序数组,数组中元素为互不相同的整数,请返回其中最小的k个数,顺序与原数组中元素顺序一致。

输入描述:

第一行为数组的长度n,需要返回的数目k,n >= k
接下来n行为数组的n个元素,每行为一个整数

输出描述:

输出为k行,每行为一个整数

示例:

输入

4 2
1
2
3
4

输出

1
2

时间限制

C/C++语言:100MS其它语言:2100MS

内存限制

C/C++语言:10KB其它语言:524298KB

我的回答

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;
            }
        }
    }
}

哔哩哔哩2019秋招技术岗编程题

(2020-08-14 刷题记录)

给定一个整数数组,判断其中是否有3个数和为N

输入描述:

输入为一行
逗号前为一个整数数组,每个元素间用空格隔开;逗号后为N

输出描述:

输出bool值
True表示存在3个和为N的数
False表示不存在3个和为N的数

示例:

输入

1 2 3 4 5,10

输出

True

备注:

数组长度不超过2000,所以数均为int范围的正整数

我的回答

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");
    }
}

扭蛋机

22娘和33娘接到了小电视君的扭蛋任务:
一共有两台扭蛋机,编号分别为扭蛋机2号和扭蛋机3号,22娘使用扭蛋机2号,33娘使用扭蛋机3号。
扭蛋机都不需要投币,但有一项特殊能力:
扭蛋机2号:如果塞x(x范围为>=0正整数)个扭蛋进去,然后就可以扭到2x+1个
扭蛋机3号:如果塞x(x范围为>=0正整数)个扭蛋进去,然后就可以扭到2x+2个
22娘和33娘手中没有扭蛋,需要你帮她们设计一个方案,两人“轮流扭”(谁先开始不限,扭到的蛋可以交给对方使用),用“最少”的次数,使她们能够最后恰好扭到N个交给小电视君。

输入描述:

输入一个正整数,表示小电视君需要的N个扭蛋。

输出描述:

输出一个字符串,每个字符表示扭蛋机,字符只能包含"2"和"3"。

示例:

输入

10

输出

233

备注:

1<=N<=1e9

我的回答

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);
    }
}

微软中国2021校招笔试题

(回忆+含部分答案)

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:

adaada

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.

猜你喜欢

转载自blog.csdn.net/qq_39517716/article/details/108368079