第一题:卖服务器,用最笨的方法A了,代码很臭很长
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[] server = new int[n]; int[][] consumer = new int[m][3]; for (int i = 0;i < n;i++) server[i] = sc.nextInt(); for (int i = 0;i < m;i++) { consumer[i][0] = sc.nextInt(); consumer[i][1] = sc.nextInt(); consumer[i][2] = 0; } Arrays.sort(server); int index = n-1; int res = 0; //如果还有客户没被处理过 while (hasConsumer(consumer)) { int max = 0; int maxIndex = -1; //优先找到没被处理过的出钱最多的客户 for (int i = 0;i < m;i++) { if (consumer[i][1] > max && consumer[i][2] == 0) { max = consumer[i][1]; maxIndex = i; } } consumer[maxIndex][2] = 1; //如果小A能够满足当前出钱最多的客户 if (hasServer(server, consumer[maxIndex][0])) { res += consumer[maxIndex][1]; } } System.out.println(res); } public static boolean hasConsumer(int[][] consumer) { for (int i = 0;i < consumer.length;i++) { if (consumer[i][2] == 0) return true; } return false; } //判断是否 public static boolean hasServer(int[] server,int cur) { for (int i = 0;i < server.length;i++) { if (server[i] >= cur) { server[i] = -1; return true; } } return false; }第二题赏金猎人不会,有好兄弟教教我怎么做吗
全部评论
(0) 回帖