【Kick Start】ATM Queue

题目思路

  • 这题的思路是从前往后一次遍历,判断出每个人需要排队几次才可以取完钱。 按照排队的次数,有序按照index排列
  • 一开始想到用hashmap存储,key是排队次数,value是人员编号的list
    但是前一阵还总结了TreeMap是一种按照key有序存储的结构,所以这里用TreeMap,key是排队次数,value是人员编号的list
  • 队次数应该用(要取的钱 - 1) / 次可取的最大钱数,这里一定要减一,如果不减一的话,当要取的钱等于可取的最大钱数的时候,排队的次数就是1次了,其实应该是0次。
  • 最后遍历一遍TreeMap

题目描述

原文
Problem
There are N people numbered from 1 to N, standing in a queue to withdraw money from an ATM. The queue is formed in ascending order of their number. The person numbered i wants to withdraw amount Ai. The maximum amount a person can withdraw at a time is X. If they need more money than X, they need to go stand at the end of the queue and wait for their turn in line. A person leaves the queue once they have withdrawn the required amount.You need to find the order in which all the people leave the queue.
Input
The first line of the input gives the number of test cases T. T test cases follow.The first line of each test case gives two space separated integers: the number of people standing in the queue, N and the maximum amount X that can be withdrawn in one turn.The next line contains N space separated integers Ai.
Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the space separated list of integers that denote the order in which the people leave the queue.
Limits
Time limit: 20 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
Test Set 1
1 ≤ N ≤ 100.
1 ≤ Ai ≤ 100.
1 ≤ X ≤ 100.
Test Set 2
1 ≤ N ≤ 105 for at most 10 test cases. For the remaining cases, 1 ≤ N ≤ 100
1 ≤ Ai ≤ 109.
1 ≤ X ≤ 109.
Sample
Input
2
3 3
2 7 4
5 6
9 10 4 7 2
Output
Case #1: 1 3 2
Case #2: 3 5 1 2 4

代码

import java.util.*;
import java.io.*;
public class Solution {
    public static void main(String[] args) {
        Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        int t = in.nextInt(); // Scanner has functions to read ints, longs, strings, chars, etc.
        for (int i = 1; i <= t; ++i) {
            TreeMap<Integer, LinkedList<Integer>> temp = new TreeMap<>();
            int people = in.nextInt();
            int money = in.nextInt();
            for(int j = 1; j <= people; ++j){
                temp.computeIfAbsent((in.nextInt() - 1) / money, k -> new LinkedList<>()).addLast(j);
            }
            StringBuilder str = new StringBuilder();
            for(LinkedList<Integer> list: temp.values()){
                for (int item : list){
                    str.append(item).append(" ");
                }
            }
            String res = str.toString();
            res = res.substring(0, res.length() - 1);
            System.out.println("Case #" + i + ": " + res);
        }
    }
}
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值