代码来自http://blog.csdn.net/cstopcoder/article/details/25559285?utm_source=tuicool&utm_medium=referral
题意:
对于给定的电话单子进行处理,注意:on必须后面有一个off匹配。而且如果这个单子不合理就不输出。PAT题里根本没叙述还有这测试点,坑死人。
思路:
自己想用map试一下,但是不太会it->second的排序,恰好网上有个写的很好的。
#include<cstdio>
#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
typedef struct Calls
{
int month;
int day;
int hour;
int minute;
string word;
}Calls;
bool cmp(const Calls &l,const Calls &r)
{
if(l.day<r.day)
return true;
else if(l.day==r.day&&l.hour<r.hour)
return true;
else if(l.day==r.day&&l.hour==r.hour&&l.minute<r.minute)
return true;
else
return false;
}
map<string,vector<Calls> > m;//存储名字对应的通话记录
int Rate[24];
float ComputeFee(Calls l,Calls r,int *totaltime)//计算通话费用
{
float fee=0;
*totaltime=0;
while(l.day<r.day||l.hour<r.hour||l.minute<r.minute)
{
fee+=Rate[l.hour];
(*totaltime)++;
l.minute++;
if(l.minute==60)
{
l.minute=0;
l.hour++;
if(l.hour==24)
{
l.hour=0;
l.day++;
}
}
}
return fee;
}
int main(int argc,char *argv[])
{
int i,j;
int n;
for(i=0;i<24;i++)
cin>>Rate[i];
cin>>n;
for(i=0;i<n;i++)
{
string name;
Calls temp;
cin>>name;
scanf("%d:%d:%d:%d",&temp.month,&temp.day,&temp.hour,&temp.minute);
cin>>temp.word;
m[name].push_back(temp);
}
map<string,vector<Calls> >::iterator it;
vector<Calls>::iterator p;
for(it=m.begin();it!=m.end();it++)
{
sort((it->second).begin(),(it->second).end(),cmp);
}
for(it=m.begin();it!=m.end();it++)
{
int FindPair=false;
int totaltime=0;
float total=0;
for(p=(it->second).begin();p!=(it->second).end();p++)
{
Calls temp=*p;
if(temp.word[1]=='n')
{
if((p+1)!=(it->second).end())
{
Calls next=*(p+1);
if(next.word[1]=='f')
{
if(!FindPair)
{
cout<<it->first;
printf(" %02d\n",p->month);
FindPair=true;
}
printf("%02d:%02d:%02d %02d:%02d:%02d",temp.day,
temp.hour,temp.minute,next.day,next.hour,next.minute);
float fee=ComputeFee(temp,next,&totaltime);
printf(" %d $%.2f\n",totaltime,fee/100);
total+=fee;
}
}
}
}
if(FindPair)
printf("Total amount: $%.2f\n",total/100);
}
return 0;
}