Kuaishou 2018 Spring Recruitment Written Test Back-end Programming Questions and Solutions

1. Calculate (x^y)%N

    Before giving the Daniel code, let’s throw it around and quote it:
import java.util.Scanner;

public class Code01 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			int x = sc.nextInt();
			int y = sc.nextInt();
			int N = sc.nextInt();
			System.out.println((int)Math.pow(x, y)%N);
		}
		sc.close();
	}

}
The pass rate is only more than 60%. The following is the code of Niuke.com, but it has not been verified whether all passed:
import java.util.Scanner;

public class Code04 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			long x = sc.nextLong(), y = sc.nextLong(), N = sc.nextLong();//Enter three numbers X, Y, Z
			long result = 1;
			x = x % N;
			while (y > 0) {
				if (y % 2 == 1)
					result = (result * x) % N;
				and /= 2;
				x = (x * x) % N;
				}
			System.out.println(result);
		}
		sc.close();
	}
}

2. Binary search

Again, give your own code first for reference:

import java.util.Scanner;

public class Code02 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			String arr = sc.nextLine();
			String[] A = arr.split(" ");
			int x = sc.nextInt();
			int result = solution(x,A);
			if (result<0) {
				System.out.println(A.length);
			}else {
				System.out.println(result);
			}
		}
		sc.close();
	}

	private static int solution(int x, String[] a) {
		// TODO Auto-generated method stub
		int num[]  = new int[a.length];
		for (int i = 0; i < a.length; i++) {
			num[i] = Integer.valueOf(a[i]);
		}
		//If all numbers in A are less than x, return -1, if all numbers in A are greater than x, return subscript 0
		if (x>num[a.length-1]) {
			return -1;
		}else if(x<num[0]){
			return 0;
		}else{
		//If x is within the range of array A, perform binary search
			int result = quicksort(x,num);
			while (result < 0) {
				x=x+1;
				result = quicksort(x, num);
			}
			return result;
		}
	}

	private static int quicksort(int x, int[] num) {
		// TODO Auto-generated method stub
		int low = 0;
		int high = num.length-1;
		while (low <= high) {
			int 	mid = low + ( high - low)/2;
			if 				(x<num[mid])		high = mid-1;
			else if 	(x>num[mid])		low = mid+1;
			else 										return mid;
		}
		return -1;
	}

}

Slightly dull, but also need a big cow town building:

import java.util.Scanner;

public class Code05 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			String input = sc.nextLine().toString();
			String[] stringArr = input.split(" ");// Space split
			int[] num = new int[stringArr.length];// Create an array of integers to store numbers
			for (int i = 0; i < num.length; i++) {
				num[i] = Integer.parseInt(stringArr[i]);
			}
			int key = sc.nextInt();// target number
			int result = findFirstEqualLarger(num, key);
			System.out.println(result);
		}
		sc.close();
	}

	private static int findFirstEqualLarger(int[] num, int key) {
		int left = 0;
		int right = num.length - 1;
		while (left <= right) {
			int mid = (left + right) / 2;
			if (num[mid] >= key) {
				right = mid - 1;
			} else {
				left = mid + 1;
			}
		}
		return left;
	}

}

3. A problem that calculates how many digits are in an integer from 1 to N (probably so)
code show as below:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Code03 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			int T = sc.nextInt();// number of data groups
			int[] num = new int[T];
			for (int i = 0; i < num.length; i++) {
				int temp = sc.nextInt();
				num[i] = temp;
			}
			for (int i = 0; i < num.length; i++) {
				if (num[i] < 10) {
					System.out.println(num[i]);
				} else {
					List<String> list = new ArrayList<>();
					for (int j = 1; j <= num[i]; j++) {
						String chr = String.valueOf(j);
						list.add(chr);
					}
					int count = 0;
					for (int j = 0; j < list.size(); j++) {
						int temp = list.get(j).length();
						count += temp;
					}
					System.out.println(count);
				}
			}
		}
		sc.close();
	}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324638887&siteId=291194637