题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1016
// 时间问题
// 选取基准,统一到一个标准之上做减法
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <algorithm>
#define SIZE 1000+10
using namespace std;
struct Call
{
string name;
int month, day, hh, mm, total;
string mark;
};
vector <Call> call_list;
vector <Call> format_list;
int N, charge[24];
// function
int chargeByTime(Call x);
int Time(Call s, Call e);
double Cost(Call s, Call e);
void Input();
bool cmp(Call a, Call b);
void show(vector<Call> v);
void formFormat();
int chargeByTime(Call x)
{
int hour = x.total/60;
int minutes = x.total%60;
int money=0;
int i;
for(i=0; i<hour; i++)
{
money += charge[i%24]*60;
}
money += charge[i%24]*minutes;
return money;
}
int Time(Call s, Call e)
{
return e.total-s.total;
}
double Cost(Call s, Call e)
{
return (chargeByTime(e)-chargeByTime(s))/100.0;
}
void Input()
{
int i;
for(i=0; i<24; i++)
{
cin >> charge[i];
}
cin >> N;
Call c;
while(N-->0)
{
getchar();
cin >> c.name;
cin >> c.month;
getchar();
cin >> c.day;
getchar();
cin >> c.hh;
getchar();
cin >> c.mm;
getchar();
cin >> c.mark;
c.total = c.day*24*60 + c.hh*60 + c.mm;
call_list.push_back(c);
}
}
bool cmp(Call a, Call b)
{
if(a.name != b.name)
return a.name<b.name;
else
{
if(a.total != b.total)
return a.total < b.total;
}
}
void formFormat()
{
string curname="";
bool isonline=false;
vector<Call>::iterator it;
for(it = call_list.begin(); it != call_list.end(); it++)
{
if(isonline== false && (*it).mark =="on-line" )
{
isonline=true;
format_list.push_back(*it);
curname = (*it).name;
}
else if(isonline == true && (*it).mark == "on-line")
{
isonline=true;
format_list.pop_back();
format_list.push_back(*it);
curname = (*it).name;
}
else if(isonline == true && (*it).mark == "off-line" && curname == (*it).name)
{
isonline = false;
format_list.push_back(*it);
}
}
// the last must be off-line
if( (*(format_list.end()-1)).mark == "on-line")
{
format_list.pop_back();
}
}
void show(vector<Call> v)
{
int i;
for(i=0; i<v.size(); i++)
{
cout << v[i].name << " ";
cout << v[i].month << ":";
cout << v[i].day << ":";
cout << v[i].hh << ":";
cout << v[i].mm << " ";
cout << v[i].mark << endl;
}
}
void Output()
{
int num=0;
double sum=0;
int i;
string curname="";
for(i=0; i<format_list.size(); i+=2)
{
num++;
if(curname != format_list[i].name)
{
if(num != 1)
{
printf("Total amount: $%.2lf\n", sum);
sum = 0;
}
curname=format_list[i].name;
printf("%s %02d\n", curname.c_str(), format_list[i].month);
}
printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2lf\n", format_list[i].day, format_list[i].hh, format_list[i].mm,
format_list[i+1].day, format_list[i+1].hh, format_list[i+1].mm, Time(format_list[i], format_list[i+1]),
Cost(format_list[i], format_list[i+1]));
sum += Cost(format_list[i], format_list[i+1]);
}
printf("Total amount: $%.2lf\n", sum);// the last one
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("E:\\in.txt", "r", stdin);
// freopen("E:\\out.txt", "w", stdout);
#endif
Input();
sort(call_list.begin(), call_list.end(), cmp);
formFormat();
Output();
return 0;
}