UVA 144 Student Grants

38 篇文章 0 订阅

The Government of Impecunia has decided to discourage tertiary students by making the payments of tertiary grants a long and time-consuming process. Each student is issued a student ID card which has a magnetically encoded strip on the back which records the payment of the student grant. This is initially set to zero. The grant has been set at 40peryearandispaidtothestudentontheworkingdaynearesttohisbirthday.(Impecuniansocietyisstillsomewhatmedievalandonlymalescontinuewithtertiaryeducation.)Thusonanygivenworkingdayupto25studentswillappearatthenearestoceoftheDepartmentofStudentSubsidiestocollecttheirgrant.ThegrantispaidbyanAutomaticTellerMachinewhichisdrivenbyareprogrammed80851chip2originallydesignedtorunthestateslotmachine.TheATMwasbuiltintheStateWorkshopsandisdesignedtobediculttorob.Itconsistsofaninteriorvaultwhereitholdsalargestockof 1 coins and an output store from which these coins are dispensed. To limit possible losses it will only move coins from the vault to the output store when that is empty. When the machine is switched on in the morning, with an empty output store, it immediately moves 1 coin into the output store. When that has been dispensed it will then move 2 coins, then 3, and so on until it reaches some preset limit k. It then recycles back to 1, then 2 and so on.
The students form a queue at this machine and, in turn, each student inserts his card. The machine dispenses what it has in its output store and updates the amount paid to that student by writing the new total on the card. If the student has not received his full grant, he removes his card and rejoins the queue at the end. If the amount in the store plus what the student has already received comes to more than 40,themachineonlypaysoutenoughtomakethetotalupto 40. Since this fact is recorded on the card, it is pointless for the student to continue queuing and he leaves. The amount remaining in the store is then available for the next student.
Write a program that will read in values of N (the number of students, 1 ≤ N ≤ 25) and k (the limit for that machine, 1 ≤ k ≤ 40) and calculate the order in which the students leave the queue.

Input
Input will consist of a series of lines each containing a value for N and k as integers. The list will be terminated by two zeroes (0 0).

Output
Output will consist of a line for each line of input and will contain the list of students in the order in which they leave the queue. Students are ordered according to their position in the queue at the start of the day. All numbers must be right justi ed in a eld of width 3.

Sample Input
5 3
0 0

Sample Output
1 3 5 2 4

题意:
有n个学生排队领钱,取款机每次从1开始按递增序列给钱,从1开始到k后又从1开始,循环往复。 如果学生领够40元就离开,否则回到队尾重新排序,如果给的钱加上学生生手中的钱多余40,则剩下的钱给下一个人,输出学生离开的顺序
思路:
建立一个student结构体,储存学号和钱数,将其依次添加到队列中,模拟整个过程

#include <iostream>
#include <queue>
#include <string>
#include <iomanip>
int N,k;
using namespace std;
struct IDnumber//存储学生的状态
{
    int num;
    int sum;
};
queue <IDnumber> queStudents;
void initial()
{
    IDnumber id;
    while(!queStudents.empty())//每次都清空队列
        queStudents.pop();
    for(int i=0;i<N;i++)
    {
        id.num=i+1;
        id.sum=0;
        queStudents.push(id);
    }
}
void payStudents()
{
    int m=1,flag=0;
    IDnumber id;
    for(int i=1;i<=k;i++)
    {
        if(!queStudents.empty())
        id=queStudents.front();//取出队列前端的对象
        else
            break;
        if(flag==0)
        m=i;
        if(id.sum+m<=40)//若小于40
        {
            id.sum+=m;//重置该学生的钱数
            flag=0;
        }
        else if(id.sum+m>40)//若大于40
        {
            m-=40-id.sum;//m为余下的钱
            id.sum=40;//重置该学生钱数为40
            flag=1;//flag标记为1
            i--;//取款机后台停止工作
        }
        queStudents.pop();
        if(id.sum<40)
            queStudents.push(id);//若钱数小于40,重新到队尾排队,添加到队列中
        else
            cout<<setw(3)<<id.num;//s输出格式,每个数字占3个字符位
        if(i==k)
            i=0;//取款机重复从1开始给钱
    }
}
int main ()
{

    while(cin>>N>>k)
    {
        if(N==0&&k==0)
            break;
        initial();
        payStudents();
        cout<<endl;
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值