uva144 Student Grants

问题描述:每年学校通过自动发款机为每个学生发放40元的补助。补助的发放过程由两个部分构成:一个是自动发款机的工作过程,二是学生取款的过程。

  自动发款机的工作过程是:发款机分为后台部分和前台部分。后台部分存有大量的1元的硬币。工作开始时后台先向前台传送1枚硬币,当硬币被分发给学生后,后台再向前台同时传送2枚硬币,硬币同时被分发后,后台再向前台同时传送3枚.....这样直到后台向前台传送的金额数达到前台的极限值k后,后台向前台传送的硬币金额数会重新变成1,再又一直到k。如此反复。设k = 3,则后台向前台传送的硬币金额数依次为:1,2,3,1,2,3,...

  学生取款的过程是:学生将记录补助金额总数的磁卡插入前台,后台向前台传送硬币数。此时会出现三种情况:

第一:如果此时前台的硬币金额数加上学生磁卡中的金额数小于40,当前台支付给学生所有金额后,此学生取出磁卡,接着到队尾再去排队领补助。

第二:如果此时前台的硬币金额数加上学生磁卡中的金额数等于40,当前台支付给学生所有金额后,此学生离队。

第三:如果此时前台的硬币金额数加上学生磁卡中的金额数大于40,前台会首先支付给学生硬币金额,当学生磁卡中金额总数为40时,此学生离队,前台会将剩余的金额支付给下一位排队的学生。

先贴我的代码,然后是老师的代码,我是根据老师思路写的,比他差远了。

 1 #include <iostream>
 2 #include <queue>
 3 #include <iomanip>
 4 using namespace std;
 5
 6 struct StuID{
 7     int IntPayNum;
 8     int CardNum;
 9 };
10 queue<StuID> queStu;
11
12 void solve(int N,int k);
13 class Machine{
14 private:
15     int intOutput;
16     int intLimit;
17     int intNextcoins;
18 public:
19     Machine(int k);//构造函数
20     void process(StuID *card);
21 };
22 Machine::Machine(int k){
23     intOutput = 1;
24     intLimit = k;
25     intNextcoins = 1;
26 }
27 void Machine::process(StuID *card){
28     if(card->IntPayNum + intOutput <= 40){
29         card->IntPayNum += intOutput;
30         intNextcoins = 1 + (intNextcoins % intLimit);//此处注意,
31         intOutput = intNextcoins;
32     }
33     else{
34         intOutput = intOutput - (40 - card->IntPayNum);
35         card->IntPayNum = 40;
36     }
37 }
38 int main()
39 {
40     //freopen("D:\\t.txt","r",stdin);
41     int N,k;
42     while((cin>>N>>k)&&!(N==0&&k==0)){
43         solve(N,k);
44         cout<<endl;
45     }
46     return 0;
47 }
48
49
50 void solve(int N,int k){
51     StuID StuForm;
52     while(!queStu.empty()){queStu.pop();}
53     for(int i = 0;i < N;i++){
54         StuForm.CardNum = i + 1;
55         StuForm.IntPayNum = 0;
56         queStu.push(StuForm);
57     }//初始化学生队列;
58     StuID student;
59     Machine get(k);
60     while(!queStu.empty()){
61         student = queStu.front();
62         queStu.pop();
63         get.process(&student);
64         if (student.IntPayNum == 40){
65             cout << setw(3) << student.CardNum;
66         }
67         else{
68             queStu.push(student);
69         }
70     }
71
72 }

第30行:此处要注意,此处是为了记忆当前台依次发放金额的多少,和学生取款时问题3相关。

下面是老师的代码:

 1 /*********************************/
 2 /* uva144 Student Grants
 3 /* Coded by Guojin ZHU
 4 /* Run Time 0.008s
 5 /* AC on July 12, 2010
 6 /*********************************/
 7 #include <iostream>
 8 #include <iomanip>
 9 #include <queue>
10 using namespace std;
11 ///////////////////////////////
12 struct IDCard{
13     int intIDNumber;
14     int intPayment;
15 };
16 class Machine{
17 private:
18     int intOutputStore;
19     int intLimit;
20     int intNextCoins;
21 public:
22     Machine(int k);
23     void makingPayment(IDCard* card);
24 };
25 Machine::Machine(int k){
26     intOutputStore = 1;
27     intLimit = k;
28     intNextCoins = 1;
29 }
30 void Machine::makingPayment(IDCard* card){
31     if ((card->intPayment + intOutputStore) <= 40){
32         card->intPayment += intOutputStore;
33         intNextCoins = 1 + (intNextCoins % intLimit);
34         intOutputStore = intNextCoins;
35     }else{
36         intOutputStore -= 40 - card->intPayment;
37         card->intPayment = 40;
38     }
39 }
40 ///////////////////////////////
41 class StudentGrants{
42 private:
43     int intNumberOfStudents;
44     int intLimitForMachine;
45     queue<IDCard> queCard;
46 public:
47     void setNumber(int n);
48     void setLimit(int k){intLimitForMachine = k;};
49     void process();
50 };
51 void StudentGrants::setNumber(int n){
52     IDCard card;
53     intNumberOfStudents = n;
54     while (!queCard.empty()){
55         queCard.pop();
56     }
57     for (int i = 0; i < intNumberOfStudents; i++){
58         card.intIDNumber = i + 1;
59         card.intPayment = 0;
60         queCard.push(card);
61     }
62 }
63 void StudentGrants::process(){
64     Machine m(intLimitForMachine);
65     IDCard card;
66     while (!queCard.empty()){
67         card = queCard.front();
68         queCard.pop();
69         m.makingPayment(&card);
70         if (card.intPayment == 40){
71             cout << setw(3) << card.intIDNumber;
72         }else{
73             queCard.push(card);
74         }
75     }
76     cout << endl;
77 }
78 ////////////////////////////////
79 int main(){
80     int n, k;
81     StudentGrants sg;
82     while((cin >> n >> k) && !((n == 0) && (k == 0))){
83         sg.setNumber(n);
84         sg.setLimit(k);
85         sg.process();
86     }
87     return 0;
88 }

看起来就佩服!

时间: 2024-08-24 11:53:28

uva144 Student Grants的相关文章

UVa 144 - Student Grants

題目:有n個學生排隊取錢,取款機每次給的錢數是一個遞增序列,從1開始到k后又從1開始: 如果學生取夠40元就離開,否則回到隊尾,如果給的錢加上學生手中的錢多餘40, 剩下的錢給下一個人,請你輸出離開的序列編號. 分析:數據結構(DS),模擬.利用隊列模擬即可,注意如果剩下的錢沒被取走,就不會出新的錢. 說明:又是好長時間沒刷題╮(╯▽╰)╭. #include <cstring> #include <cstdio> typedef struct _queue { int id; i

HOJ 题目分类

转自:http://blog.sina.com.cn/s/blog_65f3869301011a1o.html ******************************************************************************* 简单题(包括枚举,二分查找,(复杂)模拟,基础数据结构(栈.队列),杂题等 ****************************************************************************

551. 学生出席记录 Student Attendance Record I

You are given a string representing an attendance record for a student. The record only contains the following three characters: 'A' : Absent. 'L' : Late. 'P' : Present. A student could be rewarded if his attendance record doesn't contain more than o

设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1-2的表(一)~表(四)所示。用SQL语句创建四个表并完成相关题目。

表(一)Student (学生表) -- Create table create table STUDENT ( sno VARCHAR2(3) not null, sname VARCHAR2(8) not null, ssex VARCHAR2(2) not null, sbirthday DATE, class VARCHAR2(5) ) tablespace USERS pctfree 10 initrans 1 maxtrans 255; -- Add comments to the

[LeetCode] Student Attendance Record I

You are given a string representing an attendance record for a student. The record only contains the following three characters: 'A' : Absent. 'L' : Late. 'P' : Present. A student could be rewarded if his attendance record doesn't contain more than o

github student pack中的digital ocean可以使用银联卡支付

申请了 github student pack却因为一直没有visita信用卡,而无法使用digital ocean的 $50,一直到今天,用中国银行借记卡成功支付. 方法是: (1)注册paypal账号,不需要绑定银行卡或信用卡. (2)打开digital ocean的welcome页面,那里会提示需要打入$5,支付方式可选“信用卡/借记卡”或者 “paypal”,选择paypal (3)进入支付页面,登陆paypal账户,输入银联卡(我的是中国银行)的卡号,然后填入后边的一些相关信息.点击“

Spring RPC 入门学习(3)-插入Student对象

Spring RPC 向后台传递对象 1. 新建RPC接口:StudentInterface.java package com.cvicse.ump.rpc.interfaceDefine; import com.cvicse.ump.student.Student; public interface StudentInterface { public Student getStudentById(String id); public Boolean insertStudent(Student

Spring RPC 入门学习(3)-获取Student对象

Spring RPC传递对象. 1. 新建RPC接口:StudentInterface.java package com.cvicse.ump.rpc.interfaceDefine; import com.cvicse.ump.student.Student; public interface StudentInterface { public Student getStudentById(String id); } 2.新建RPC接口的实现类,StudentManager.java pack

1047. Student List for Course (25)

题目例如以下: Zhejiang University has 40000 students and provides 2500 courses. Now given the registered course list of each student, you are supposed to output the student name lists of all the courses. Input Specification: Each input file contains one te