pat 甲级测试题目 -- 1016 Phone Bills
题目链接 题目描述要求计算银行账单。 输出 注意: 分析看到这道题目之后,我考虑过单纯的使用 map 或者结构体存储数据,最后确定用 map<string,vector
实现#include <iostream> #include <vector> #include <map> #include <algorithm> #include <string> using namespace std; struct node { string name; int month,day,hour,minute,statue,time; // 定义 time 用于比较时间 }; bool cmp(node a,node b) { return a.name == b.name ? a.time < b.time : a.name < b.name; // 按照姓名比较,姓名相同按照时间比较,方便计算费用 } // 计算总时间的话费,用于解决跨天问题 double CallSpend(node cus,int* rate){ double total = rate[cus.hour] * cus.minute + cus.day * rate[24] * 60; for (int i = 0; i < cus.hour; ++i) { total += rate[i] * 60; } return total/100; } int main() { int rate[25] = {0},n; // 接收计费数据 for (int i = 0; i < 24; ++i) { cin >> rate[i]; rate[24] += rate[i]; // 将一天的话费数据计算出来,用于解决跨天计算话费的问题 } cin >> n; vector<node> custom(n); for (int i = 0; i < n; ++i) { cin >> custom[i].name; scanf("%d:%d:%d:%d",&custom[i].month,&custom[i].day,&custom[i].hour,&custom[i].minute); string stemp; cin >> stemp; custom[i].statue = ((stemp == "on-line") ? 1 : 0); custom[i].time = custom[i].day * 24 * 60 + custom[i].hour * 60 + custom[i].minute; } // 对顾客数据进行排序 sort(custom.begin(),custom.end(),cmp); // 用 map 存储数据,将同一个顾客的月账单统一 map<string,vector<node>> list; for (int i = 1; i < n; ++i) { if (custom[i].name == custom[i - 1].name && custom[i - 1].statue == 1 && custom[i].statue == 0) { list[custom[i - 1].name].push_back(custom[i - 1]); list[custom[i].name].push_back(custom[i]); } } // 遍历 map 输出账单 for (auto li : list) { vector<node> temp = li.second; cout << li.first; printf(" %02dn",temp.front().month); double total = 0; for (int i = 1; i < temp.size(); i+=2) { // 计算一次通话记录的话费 double t = CallSpend(temp[i],rate) - CallSpend(temp[i-1],rate); printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2fn",temp[i-1].day,temp[i-1].hour,temp[i-1].minute,temp[i].day,temp[i].hour,temp[i].minute,temp[i].time - temp[i-1].time,t); total += t; } printf("Total amount: $%.2fn",total); } return 0; } 希望能帮到大家 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |