今晚七点快手笔试 java题解
幂数
package kuaishou; import java.util.Scanner; //(x^y) % N public class One { public static void main(String[] args) { Scanner in = new Scanner(System.in); int x = in.nextInt(); int y = in.nextInt(); int n = in.nextInt(); int res = x; for (int i = 1; i < y; i++) { res = (res*x)%n; } System.out.println(res); } }
二分查找
package kuaishou; import java.util.Scanner; public class Two { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()){ String[] str = in.nextLine().split(" "); int[] num = new int[str.length]; for (int i = 0; i < num.length; i++) { num[i] = str[i].charAt(0)-'0'; } int x = Integer.parseInt(in.nextLine()); int low = 0; int high = num.length-1; boolean flag = false; for (int i = 0; i < num.length; i++) { if (x<=num[i]){ flag = true; break; } } if (!flag){ System.out.println(num.length); continue; } while (low<=high){ int mid = (low+high)/2; if (num[mid]<x){ low = mid+1; }else if (num[mid]>x){ high = mid-1; }else { System.out.println(mid); break; } if (low>high){ System.out.println(mid); } } } } }
然而我第一道题没做就交卷了,有点心累。
#春招##实习##笔试题目#