PAT (Advanced Level) Practice - 1016 Phone Bills(25 分)

简介: PAT (Advanced Level) Practice - 1016 Phone Bills(25 分)

题目链接:点击打开链接

题目大意:略。

解题思路:

  1. 筛选合格数据:将给出的数据先按照姓名排序,再按照时间的先后顺序排列,这样遍历的时候,前后两个名字相同且前面的状态为on-line后面一个的状态为off-line的就是合格数据。
  2. 计算费用:一律用差分来做,中间的跨天来用 cst[24] * day * 60 来统计。

AC 代码

#include<bits/stdc++.h>#include<cmath>#define mem(a,b) memset(a,b,sizeof a)#define ssclr(ss) ss.clear(), ss.str("")#define INF 0x3f3f3f3f#define MOD 1000000007usingnamespacestd;
typedeflonglongll;
structnode{
stringname;
intsta,time,MM,dd,hh,mm;
};
intcst[25];
vector<node>data(1009);
intcmp(noden1,noden2)
{
returnn1.name!=n2.name?n1.name<n2.name : n1.time<n2.time;
}
doublecalBill(nodend)
{
doublers=cst[nd.hh]*nd.mm+cst[24]*60*nd.dd;
for(inti=0;i<nd.hh;i++) rs+=cst[i]*60;
returnrs/100;
}
intmain()
{
intn;
charop[15];
for(inti=0;i<24;i++) scanf("%d",&cst[i]), cst[24]+=cst[i];
scanf("%d",&n);
for(inti=0;i<n;i++)
    {
cin>>data[i].name;
scanf("%d:%d:%d:%d%s",&data[i].MM,&data[i].dd,&data[i].hh,&data[i].mm,op);
data[i].sta=op[1]=='n'?1:0;
data[i].time=data[i].dd*24*60+data[i].hh*60+data[i].mm;
    }
sort(data.begin(),data.begin()+n,cmp);
map<string,vector<node>>mp;
for(inti=1;i<n;i++)
if(data[i].name==data[i-1].name&&data[i].sta==0&&data[i-1].sta==1)
        {
mp[data[i-1].name].push_back(data[i-1]);
mp[data[i].name].push_back(data[i]);
        }
for(autoit:mp)
    {
vector<node>nd=it.second;
printf("%s %02d\n",it.first.c_str(),nd[0].MM);
doublesum=0, ans;
for(inti=1;i<nd.size();i+=2)
        {
ans=calBill(nd[i])-calBill(nd[i-1]);
sum+=ans;
printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n",nd[i-1].dd,nd[i-1].hh,nd[i-1].mm,nd[i].dd,nd[i].hh,nd[i].mm,nd[i].time-nd[i-1].time,ans);
        }
printf("Total amount: $%.2f\n",sum);
    }
return0;
}
目录
相关文章
|
移动开发 C语言
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分)
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分)
95 0
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
119 0
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
130 0
PAT (Advanced Level) Practice - 1139 First Contact(30 分)
PAT (Advanced Level) Practice - 1139 First Contact(30 分)
112 0
|
存储
PAT (Advanced Level) Practice - 1126 Eulerian Path(25 分)
PAT (Advanced Level) Practice - 1126 Eulerian Path(25 分)
138 0
PAT (Advanced Level) Practice - 1146 Topological Order(25 分)
PAT (Advanced Level) Practice - 1146 Topological Order(25 分)
86 0
PAT (Advanced Level) Practice - 1062 Talent and Virtue(25 分)
PAT (Advanced Level) Practice - 1062 Talent and Virtue(25 分)
140 0
PAT (Advanced Level) Practice - 1026 Table Tennis(30 分)
PAT (Advanced Level) Practice - 1026 Table Tennis(30 分)
126 0
|
存储 测试技术
PAT (Advanced Level) Practice - 1131 Subway Map(30 分)
PAT (Advanced Level) Practice - 1131 Subway Map(30 分)
112 0
PAT (Advanced Level) Practice - 1129 Recommendation System(25 分)
PAT (Advanced Level) Practice - 1129 Recommendation System(25 分)
103 0