PAT 1016(排序+模拟)
来源:互联网 发布:mac版的炒股软件 编辑:程序博客网 时间:2024/06/07 20:02
题目
https://www.patest.cn/contests/pat-a-practise/1016
给出一份通话记录流水账,每条记录都标记客户姓名,是开始还是结束拨打,以及对应的时间(月日小时分),统计每个人的账单,按要求输出。
解题思路
这实在是一道复杂的模拟题,要求的输出格式也是真的烦,不涉及到任何算法,纯粹要做好统计工作。
题目重点
首先划一下题目中的重点(ง •_•)ง:
- 【a long-distance call costs a certain amount per minute, depending on the time of day when the call is made.】话费与拨打开始时间、持续时间都相关,计费标准随着开始拨打的时间变动,比如00:00~01:00价格为10美分/分钟,01:00~02:00价格为20美分/分钟等等,以此类推
- 【Each “on-line” record is paired with the chronologically next record for the same customer provided it is an “off-line” record】每条通话的on-line与时间序上最近的一条off-line记录匹配
- 【Any “on-line” records that are not paired with an “off-line” record are ignored, as are “off-line” records not paired with an “on-line” record. 】补充说明不满足第2条的记录都属于无效记录,不计入。
- 【Bills must be printed in alphabetical order of customers’ names】输出时要对顾客姓名排序
尤其是第2点,可以说是本题的核心了。例如像on-on-off-off-off这种出现的情况,只取中间的on-off对。
思路和实现:
- 输入:为每条记录构造结构体,通过%2d控制月日时分的读入
- 排序:对所有记录排序,排序规则为先对不同人的姓名排序,对于同一个人的记录按时间排序
- 匹配on-off对:对排序后的所有记录,按照上述重点2和3做筛选,尝试将所有on记录压入一个vector(为节约空间,只存记录的下标),一旦出现一个新的on则覆盖之前的on记录,出现off记录时与尾端on形成匹配并压入vector 【重点】
- 输出on-off对:根据vector中的下标,索引到记录并按格式输出
- 计算价格和累加:通过模拟时间的流动,开始记录的分钟自增直到结束记录的分钟,每次自增都加上相应的分钟耗费。【重点】
需要注意的是,测试点2、3、4都考察一个点,即在匹配on-off时,可能出现尾随的多条无效on记录,它们不对应到任何off,因此输出前必须pop掉。
AC代码
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <list>using namespace std;typedef struct node{ char name[25]; int month,day,hour,minute; bool isOn;}record;int hourCost[25]; //[0..23]record rec[1005]; //读入的通话记录vector<int> output; //满足筛选条件的记录对的下标bool cmp(const record &a, const record &b) //对读入的所有记录排序{ int tmp = strcmp(a.name, b.name); if (tmp == 0) //名字相同则按时间排序 { if (a.month != b.month) return a.month < b.month; if (a.day != b.day) return a.day < b.day; if (a.hour != b.hour) return a.hour < b.hour; return a.minute < b.minute; } return tmp < 0; //名字不同则按名字升序}void testSort(int n) //输出排序后的结果{ for (int i = 0; i<n; ++i) { printf("%s %d:%d:%d:%d ", rec[i].name, rec[i].month, rec[i].day, rec[i].hour, rec[i].minute); if(rec[i].isOn) printf("on-line\n"); else printf("off-line\n"); }}void calc_cost(const record &s, const record &e, int &last, int &money){ record tmp; tmp.day = s.day; tmp.hour = s.hour; tmp.minute = s.minute; while (tmp.day < e.day || tmp.hour < e.hour || tmp.minute < e.minute) //模拟时间流动,从start直到end { money += hourCost[tmp.hour]; tmp.minute++; //分钟流动 last++; //耗时+1 if (tmp.minute > 59) { tmp.minute = 0; tmp.hour++; if (tmp.hour > 23) { tmp.hour = 0; tmp.day++; } } }}int main(){ for (int i = 0; i < 24; ++i) cin >> hourCost[i]; int n; cin >> n; char s[10]; for (int i = 0; i<n; ++i) { scanf("%s%2d:%2d:%2d:%2d %s", rec[i].name, &rec[i].month, &rec[i].day, &rec[i].hour, &rec[i].minute, s); if (strcmp("on-line", s) == 0) rec[i].isOn = true; else rec[i].isOn = false; } sort(rec, rec+n, cmp); //名字-时间升序 //testSort(n); bool hasOn = false; //之前是否出现过on-line记录 char name[25]; int i = 0; while(i < n) //只处理下面三种情况 { if (rec[i].isOn == true && hasOn == false) //当前的第一个on { hasOn = true; output.push_back(i); strcpy(name, rec[i].name); } else if(rec[i].isOn == true && hasOn == true) //再次出现on,抛弃之前的on { output.pop_back(); output.push_back(i); strcpy(name, rec[i].name); } else if (rec[i].isOn == false && hasOn == true && strcmp(name, rec[i].name) == 0) //成功匹配 { output.push_back(i); hasOn = false; } //其余情况都过滤掉 ++i; } for (int i = output.size()-1; i>=0; --i) //去掉尾随的无效开始记录,测试点2.3.4 { if (rec[output[i]].isOn == true) output.pop_back(); else break; } int m = rec[0].month; //按格式输出,并计算每一对记录的价格 record pre, now; strcpy(pre.name, "\0"); int tot_cost = 0; for (int i = 0; i < output.size(); ++i) { now = rec[output[i]]; if (strcmp(pre.name, now.name) != 0) { if (i != 0) printf("Total amount: $%.2f\n", tot_cost*1.0/100); //结束上一人的累加 tot_cost = 0; printf("%s ", now.name); printf("%02d\n", m); } printf("%02d:%02d:%02d ", now.day, now.hour, now.minute); if (now.isOn == false) { int last = 0, money = 0; calc_cost(pre, now, last, money); //本条通话记录的价格 printf("%d $%.2f\n", last, money*1.0/100); //持续时间 tot_cost += money; } pre = now; } printf("Total amount: $%.2f\n", tot_cost*1.0/100); //最后一个人的总价 return 0;}
阅读全文
0 0
- PAT 1016(排序+模拟)
- pat 1080. Graduate Admission(排序模拟)
- PAT 1016 Phone Bills(模拟)
- PAT-A1075. PAT Judge (25)(排序)
- PAT 1025 PAT Ranking(双重排序)
- 快速排序(PAT-1045)
- PAT 1015. 装箱问题模拟(20)
- PAT-A1042 Shuffling Machine(简单模拟)
- PAT-A1046 Shortest Distance(模拟)
- PAT-A1002A+B for Polynomials(模拟)
- PAT-A1061 Dating (20)(模拟)
- PAT-A1001A+B Format (20)(模拟)
- PAT-A1035. Password (20)(模拟)
- PAT-A1077. Kuchiguse (20)(模拟)
- PAT-A1084. Broken Keyboard (20)(模拟)
- PAT-A1073. Scientific Notation (20)(模拟)
- PAT 1014 Waiting in Line (模拟)
- PAT 1017 Queueing at Bank (模拟)
- 使用Caffe进行图片分类
- Markdown学习
- Java--Iterator
- history、location、document对象
- Linux虚拟机连不上网
- PAT 1016(排序+模拟)
- RSETclient和postman--两个常用数据接口测试工具
- Magento开发教程
- tcp发送和接收的原理
- TINA_PRO_6中文版.rar
- IMWeb提升营Day5 | 训练题26:二叉搜索树与双向链表
- 统计文章相同单词
- POJ 1328 Radar Installation (贪心)
- Qt5.8 ubuntu下面连接mysql