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 $40 per year and is paid to the student on the working day nearest to his birthday. (Impecunian society is still somewhat medieval and only males continue with tertiary education.) Thus on any given working day up to 25 students will appear at the nearest office of the Department of Student Subsidies to collect their grant. The grant is paid by an Automatic Teller Machine which is driven by a reprogrammed chip originally designed to run the state slot machine. The ATM was built in the State Workshops and is designed to be difficult to rob. It consists of an interior vault where it holds a large stock of $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, the machine only pays out enough to make the total up to $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 and OutputInput 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 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 justified in a field of width 3. Sample input5 3 0 0 Sample output1 3 5 2 4 |
题意: 输入n和k ,n表示人数编号为1--n,k表示最大钱数 。
取得钱的数目变化为 1 、2 、 3、 .... 、 k 、 1 、2 、 3、 .... 、 k ....
开始时 人从编号小到大的排队在ATM处取钱 , 取完钱后,若钱 >= 40,那么他就离开,并
且后面的人就取前面人剩下的钱(不为0)(若当前人有a元,他取了b元,a+b>40 那么下一个人
取得钱为 a+b-40 且总钱数 < 40 ,而下下一个人取得钱为(b+1)%k+1 而不是 b%k+1)(这儿要注意)
若钱<40,那么他就排到队的最后,等待下一次取钱。
最后输出 人离开的编号顺序。
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
struct node
{
int no,num;
void fun(int _no,int _num)
{
no=_no,num=_num;
}
};
int n,k;
void solve()
{
queue <node> q;
node a;
int cur=1,tmp;
for(int i=1;i<=n;i++)
{
a.fun(i,cur);
if(a.num>=40) printf("%3d",i);
else q.push(a);
cur=cur%k+1;
}
tmp=cur;
while(!q.empty())
{
node c=q.front();q.pop();
if(c.num+tmp>=40)
{
printf("%3d",c.no);
tmp=c.num+tmp-40;
}
else
{
c.num+=tmp;
q.push(c);
tmp=0;
}
if(tmp==0) tmp=(cur++)%k+1;
}
printf("\n");
}
int main()
{
while(scanf("%d %d",&n,&k)!=EOF)
{
if(n==0 && k==0) break;
solve();
}
return 0;
}