在题库中随机选题java_TZOJ 挑战题库随机训练08(JAVA)

本文介绍了如何使用Java实现从题库中随机选择题目,并提供了三种算法的详细解析:工作减量问题、收集传呼机问题和猜数字游戏。每种算法都涉及不同的数学和计算机科学概念,如最小成本计算、爆搜和模7的连续子序列。此外,还讨论了代码实现,包括排序和比较策略。
摘要由CSDN通过智能技术生成

点击题号跳转

A.Work Reduction回到顶部

题意

n变成m,两种操作,减1花费A,整除2花费B,问最小花费

题解

n变成n/2,操作1需要花费(n-n/2)*A,操作2需要花费B,比大小决定,复杂度O(logn)

代码

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 import java.util.*;2

3 public classMain {4

5 public static voidmain(String[] args) {6

7 Scanner sc = newScanner(System.in);8 Solution solver = newSolution();9 int t =sc.nextInt();10 for (int ca = 1; ca <= t; ca++) {11 System.out.println("Case " +ca);12 solver.solve(sc);13 }14 }15

16 }17

18 classSolution {19

20 classNode {21 String name;22 intsumCost;23

24 Node(String name, intsumCost) {25 this.name =name;26 this.sumCost =sumCost;27 }28 }29

30 public voidsolve(Scanner sc) {31 int n =sc.nextInt();32 int m =sc.nextInt();33 int agencies =sc.nextInt();34 sc.nextLine();//读回车

35 List list = new ArrayList();36 for (int i = 0; i < agencies; i++) {37 String s =sc.nextLine();38 String[] str = s.split(":");39 String[] str1 = str[1].split(",");40 int aCost = Integer.valueOf(str1[0]);41 int bCost = Integer.valueOf(str1[1]);42 int val =n;43 int sumCost = 0;44 while (val >m) {45 int updateTo = val / 2;46 int aSumCost = aCost * (val -updateTo);47 if (updateTo >= m && aSumCost >bCost) {48 sumCost +=bCost;49 val =updateTo;50 } else{51 //剩下一段全A

52 sumCost += (val - m) *aCost;53 break;54 }55 }56 list.add(new Node(str[0], sumCost));57 }58

59 Collections.sort(list, new Comparator() {60 public intcompare(Node o1, Node o2) {61 if (o1.sumCost >o2.sumCost)62 return 1;63 else if (o1.sumCost ==o2.sumCost) {64 if (o1.name.compareTo(o2.name) > 0)65 return 1;66 else

67 return -1;68 }69 else return -1;70 }71 });72

73 for (int i = 0; i < list.size(); i++)74 System.out.println(list.get(i).name + " " +list.get(i).sumCost);75 }76

77 }

A

B.Collecting Beepers

题意

10个点,给个起点,问经过每个点再回到起点的最小距离

题解

爆搜,最后加上到起点的距离,复杂度O(2^10)

代码

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 import java.util.*;2

3 public classMain {4

5 public static voidmain(String[] args) {6

7 Scanner sc = newScanner(System.in);8 Solution solver = newSolution();9 int t =sc.nextInt();10 for (int ca = 1; ca <= t; ca++) {11 solver.solve(sc);12 }13 }14

15 }16

17 classSolution {18

19 private int[] x;20 private int[] y;21 private boolean[] vis;22 private intq;23 private intsx, sy;24 private intmin;25

26 public voidsolve(Scanner sc) {27 int n =sc.nextInt();28 int m =sc.nextInt();29 sx =sc.nextInt();30 sy =sc.nextInt();31 q =sc.nextInt();32 x = new int[q];33 y = new int[q];34 vis = new boolean[q];35 min = 1000000000;36 for (int i = 0; i < q; i++) {37 x[i] =sc.nextInt();38 y[i] =sc.nextInt();39 }40 dfs(0, 0, sx, sy);41 System.out.println("The shortest path has length " +min);42 }43

44 private void dfs(int step, int cost, int ux, intuy) {45 if (step ==q) {46 min = Math.min(min, c

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值