腾讯开发岗笔试题解Java
第一题:就是一个找规律
public class Q1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long n = sc.nextInt(); long m = sc.nextInt(); long count = m * m; long sum = n / (2 * m) * count; System.out.println(sum); } }
第二题:dp
public class Q2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int K = sc.nextInt(); int A = sc.nextInt(); int X = sc.nextInt(); int B = sc.nextInt(); int Y = sc.nextInt(); int[][] dp = new int[X+Y+1][K+1]; for (int i = 0; i < X+Y+1; i++) dp[i][0] = 1; for (int i = 1; i < X+Y+1; i++) { for (int j = 1; j <= K; j++) { if (i <= X) { if (j >= A) dp[i][j] = (dp[i-1][j] + dp[i-1][j-A]) % 1000000007; else dp[i][j] = dp[i-1][j] % 1000000007; } else if (i <= X+Y) { if (j >= B) dp[i][j] = (dp[i-1][j] + dp[i-1][j-B]) % 1000000007; else dp[i][j] = dp[i-1][j] % 1000000007; } } } System.out.println(dp[X+Y][K]); } }
第三题:贪心,排序求和
public class Q3 { static class Pair { int time; int level; public Pair(int time, int level) { this.time = time; this.level = level; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); Pair[] machine = new Pair[n]; Pair[] task = new Pair[m]; // input for (int i = 0; i < n; i++) machine[i] = new Pair(sc.nextInt(), sc.nextInt()); for (int i = 0; i < m; i++) task[i] = new Pair(sc.nextInt(), sc.nextInt()); int[] cnt = new int[105]; // sort Comparator<Pair> comparator = (a, b) -> { if (a.time == b.time) return b.level - a.level; else return b.time - a.time; }; Arrays.sort(machine, comparator); Arrays.sort(task, comparator); long sum = 0; int j = 0, cnt1 = 0; for(int i = 0;i < m;i++) { while(j < n && machine[j].time >= task[i].time) { cnt[machine[j].level]++; j++; } for(int k = task[i].level; k < 101; k++) { if(cnt[k] != 0) { cnt[k]--; sum += 200*task[i].time + 3*task[i].level; cnt1++; break; } } } System.out.println(cnt1 + " " + sum); } }#笔试题目##腾讯##Java#