版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/w419387229/article/details/81535459
这种比较麻烦的数据处理考察语法吧感觉。
map,set这两个结构会自动进行排序。因此对此题很便利。
总的来说这题如果语法,库都比较熟悉就很简单,由于有些结构没怎么用过,因此照着网上的代码敲了遍。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
int rate[25];
int n;
struct Detail{
int dd, hh, mm;
string status;
Detail(){}
Detail(int dd, int hh, int mm, string status) : dd(dd), hh(hh),mm(mm), status(status){}
bool operator < (const Detail &b) const {
if(dd != b.dd)
return dd < b.dd;
else{
if(hh != b.hh)
return hh < b.hh;
else
return mm < b.mm;
}
}
};
map<pair<string,int>,set<Detail> > mp;
int calculate(const Detail &a, const Detail &b){
printf("%02d:%02d:%02d %02d:%02d:%02d ", a.dd, a.hh, a.mm, b.dd, b.hh, b.mm);
int cost = -a.mm*rate[a.hh] + b.mm*rate[b.hh];//把前面的时间归零,后面的时间加上
int time = -a.mm + b.mm;//时间同理
int tmph = a.hh, tmpd = a.dd;
//先处理天数,前面归零的时间,也在这里被补上了
while(tmpd < b.dd){
do{
cost += 60*rate[tmph];
tmph++;
tmph %= 24;
time += 60;
}while(tmph != 0);
tmpd++;
}
//同一天了,此时 tmph = 0
while(tmph < b.hh){
cost += 60*rate[tmph];
time += 60;
tmph++;
}
printf("%d $%.2f\n",time,(float)cost/100);
return cost;
}
void print(string name, int month, set<Detail> &st){
int total = 0, online = 0;
Detail tmp;
for(set<Detail>::iterator it= st.begin(); it != st.end(); it++){
if(it->status == "on-line"){
tmp = *it;
if(!online){
online = 1;
}
}
else{
if(online){
if(total == 0){
cout << name;
printf(" %02d\n",month);
}
total += calculate(tmp, *it);
online = 0;
}
}
}
if(total)
printf("Total amount: $%.2f\n",(float)total/100);
}
int main(){
for(int i = 0; i < 24; i++){
scanf("%d",&rate[i]);
}
scanf("%d",&n);
for(int i = 0; i < n; i++){
string name, status;
int M,d,h,m;
cin >> name;
scanf("%d:%d:%d:%d",&M,&d,&h,&m);
cin >> status;
mp[make_pair(name,M)].insert(Detail(d,h,m,status));
}
for(map<pair<string,int>,set<Detail> >::iterator it = mp.begin(); it != mp.end(); it++){
print(it->first.first, it->first.second, it->second);
}
return 0;
}