兴业数金笔试
选择题:存储过程、异常捕获、交换机的功能、序列化、线程优先级、SpringMVC、yield、数组等
代码题:分解质因子。
由于之前在做笔试时遇到了求素数的题,所以就回忆了下线性素数筛,这次笔试就用到了。
题干描述不充分的地方是,没有给待分解整数的范围。
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); StringBuilder cache = new StringBuilder(); cache.append(num).append("="); if (num < 4) { cache.append(num); } else { List<Integer> primeList = getPrime(num); int i = 0; while (num % primeList.get(i) != 0) { ++i; } cache.append(primeList.get(i)); num = num / primeList.get(i); while(num != 1) { while (num % primeList.get(i) != 0) { ++i; } cache.append("*").append(primeList.get(i)); num /= primeList.get(i); } } System.out.println(cache); } static List<Integer> getPrime(int num) { int m = (int)Math.sqrt(num + 0.5); boolean[] vis = new boolean[num + 1]; for (int i = 2; i <= m; ++i) { if (!vis[i]) { for (int j = i * i; j <= num; j += i) { vis[j] = true; } } } List<Integer> ret = new ArrayList<>(); for (int i = 2; i <= num; ++i) { if (!vis[i]) { ret.add(i); } } return ret; } }