UVA 144 Student Grants
来源:互联网 发布:长兴人民法院淘宝拍卖 编辑:程序博客网 时间:2024/06/08 07:05
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
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
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;}
- UVa 144 - Student Grants
- UVA 144 Student Grants
- uva 144 Student Grants(队列模拟过程)
- UVa144 Student Grants
- Shipping Grants
- student
- student
- student
- student
- student
- SGU 318 Grants
- Mysql账号权限查询(grants)
- mysql show grants查看权限
- New* Government Grants & Free U.s Money.
- Student.java
- student.class
- csdn student
- student 类库
- 如何判断2条线段是否相交(编程实现)?
- Nginx ngx_http_referer_module模块根据referer屏蔽或禁止访问
- PAT 甲级 1069. The Black Hole of Numbers (20)
- java-工厂模式
- 字段级
- UVA 144 Student Grants
- 《数据结构与算法-Python语言描述》读书笔记(5)第5章栈和队列(关键词:数据结构/算法/Python/栈/队列)
- 排序算法之懒人排序
- Qt学习: QListWidget程序示例
- linux设备编程笔记(1)
- Linux命令由来-辅助记忆
- strcmp和memcmp两个字符串比较函数
- (面试题三)【JAVAse部分】面向对象相关
- 关于两个php集成环境apache和数据库端口冲突的问题