快手2018春招后端笔试题解
计算(x^y)%N
package kuaishou.demo1;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long x = sc.nextLong(), y = sc.nextLong(), N = sc.nextLong();
long res = 1;
x = x % N;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % N;
y /= 2;
x = (x * x) % N;
}
System.out.println(res);
}
}
二分查找
package kuaishou.demo2;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String inputString = sc.nextLine().toString();
String stringArray[] = inputString.split(" ");
int num[] = new int[stringArray.length];
for (int i = 0; i < stringArray.length; i++) {
num[i] = Integer.parseInt(stringArray[i]);
}
int key = sc.nextInt();
System.out.println(findFirstEqualLarger(num, key));
}
public static int findFirstEqualLarger(int[] array, int key) {
int left = 0;
int right = array.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (array[mid] >= key) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return left;
}
}
#笔试题目##实习##春招#