1016. Phone Bills (25)
来源:互联网 发布:淘宝客返利怎么设置 编辑:程序博客网 时间:2024/06/01 19:50
题目连接:https://www.patest.cn/contests/pat-a-practise/1016
题目大意:给出一系列的电话记录,这些记录每个通话接通的时刻和挂断的时刻。从中找出完整的通话记录(同时含接通时刻和挂断时刻),然后输出,输出按照姓名排序,同一个人的按照时间先后排序
解题思路:思路不难,但比较繁琐。将所有记录按照姓名排序,姓名相同的按照时间排序。然后从记录中挑出符合条件的记录对。
代码如下:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <map>using namespace std;struct record{ string name;//记录的姓名 int month,day,hour,minute;//记录的时间 bool flag;//接通还是挂断};double compute(record r,int *rate){//用来计算钱,以00:00:00为基准.计算花费的时候使用差值 double total=rate[r.hour]*r.minute+rate[24]*60*r.day; for (int i=0;i<r.hour;i++) total+=rate[i]*60; return total/100.0;}bool cmp1(record a,record b){//先按名字排序,名字相同的按照时间排序 if(a.name==b.name){ if(a.day==b.day){ if(a.hour==b.hour) return a.minute<b.minute; else return a.hour<b.hour; } else return a.day<b.day; } else return a.name<b.name;}int main(int argc, char const *argv[]){ int toll[25]={0}; for(int i=0;i<24;i++){ cin>>toll[i]; toll[24]+=toll[i]; } int N; cin>>N; record buf[N]; string offon; for(int i=0;i<N;i++){//开始输入记录 cin>>buf[i].name; scanf("%02d:%02d:%02d:%02d",&buf[i].month,&buf[i].day,&buf[i].hour,&buf[i].minute); cin>>offon; if(offon=="on-line") buf[i].flag=true; else buf[i].flag=false; } sort(buf,buf+N,cmp1); map<string,vector<record>> m_map;//键为名字,值为该名字的所有记录 for(int i=1;i<N;i++){//将能够配对成功的记录保存在map中 if(buf[i].name==buf[i-1].name&&buf[i].flag==false&&buf[i-1].flag==true){//配对成功 m_map[buf[i-1].name].push_back(buf[i-1]); m_map[buf[i].name].push_back(buf[i]); } } map<string,vector<record>>::iterator it; for(it=m_map.begin();it!=m_map.end();it++){ cout<<it->first;//输出名字 vector<record> R=it->second;//改名字的所有配对记录 printf(" %02d\n",R[0].month);//输出月份 double total=0;//该人的所有记录的总花费 for(int i=0;i<R.size()-1;i+=2){//遍历每个记录对 double price=compute(R[i+1],toll)-compute(R[i],toll); int t=(R[i+1].day*24+R[i+1].hour)*60+R[i+1].minute-(R[i].day*24+R[i].hour)*60-R[i].minute; printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n",R[i].day,R[i].hour,R[i].minute,R[i+1].day,R[i+1].hour,R[i+1].minute,t,price); total+=price; } printf("Total amount: $%.2f\n", total); } return 0;}
阅读全文
0 0
- 1016. Phone Bills (25)
- 1016. Phone Bills (25)
- 1016. Phone Bills (25)
- 1016. Phone Bills (25)
- 1016. Phone Bills (25)
- 1016. Phone Bills (25)
- 1016. Phone Bills (25)
- 1016. Phone Bills (25)
- 1016. Phone Bills (25)
- 1016. Phone Bills (25)
- 1016. Phone Bills (25)
- 1016. Phone Bills (25)
- 1016. Phone Bills (25)
- 1016. Phone Bills (25)
- 1016. Phone Bills (25)
- 1016. Phone Bills (25)
- 1016. Phone Bills (25)
- 1016. Phone Bills (25)
- 最大连续子序列求和
- LeetCode:M59. Spiral Matrix II
- 个人软件开发职业技能计划书
- c++ set
- python函数
- 1016. Phone Bills (25)
- JAVA第七章-集合(三)
- 初学R语言
- Java中Comparable和Comparator区别小结
- 制作动图——imagemagick
- cake (gcd)【HDU】-1722
- 读书笔记——深入理解java虚拟机
- python爬虫入门(1) 基础知识 ; 正则表达式 Re 模块
- Cow Hurdles POJ-3615-------没想到不会TLE的Floyd算法