快手2018春招笔试后端编程题题解

1、计算(x^y)%N

    在给出大牛代码之前,先抛转引玉一下:
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();
	}

}
通过率只有60%多,下面给出牛客网一大牛代码,但未验证是否全部通过:
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();//输入三个数X,Y,Z
			long result = 1;
			x = x % N;
			while (y > 0) {
				if (y % 2 == 1)
					result = (result * x) % N;
				y /= 2;
				x = (x * x) % N;
				}
			System.out.println(result);
		}
		sc.close();
	}
}

2.二分查找

同样,还是先给出自己的代码,以供参考:

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]);
		}
		//如果A中所有数字都小于x,则返回-1,,如果A中的数字都大于x,则,返回下标0
		if (x>num[a.length-1]) {
			return -1;
		}else if(x<num[0]){
			return 0;
		}else{
		//如果x在数组A的范围之内,则执行二分查找
			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;
	}

}

稍显愚钝,还需大牛镇楼:

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(" ");// 空格拆分
			int[] num = new int[stringArr.length];// 创建整型数组,存储数字
			for (int i = 0; i < num.length; i++) {
				num[i] = Integer.parseInt(stringArr[i]);
			}
			int key = sc.nextInt();// 目标数字
			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.一个计算从1到N的整数有多少位的题目(大概是这样)
代码如下:

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();// 数据组数
			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();
	}

}

猜你喜欢

转载自blog.csdn.net/ARPOSPF/article/details/80038873
今日推荐