注意审题:“It is guaranteed that at least one call is well paired in the input.” 只是说了输入中有配对的记录,但并没有说每个人都有配对的记录,因此存在个人话费为0的情况,此时什么都不需要输出(包括名字)
#include <bits/stdc++.h>
using namespace std;
typedef struct
{
string name;
int stamp;
int status;
} record;
typedef struct
{
int MM;
int dd;
int HH;
int mm;
} times;
int times2stamp(times t)
{
return t.mm + t.HH * 60 + t.dd * 60 * 24 + t.MM * 60 * 24 * 31;
}
times stamp2times(int stamp)
{
times t;
t.mm = stamp % 60;
t.HH = stamp / 60 % 24;
t.dd = stamp / 60 / 24 % 31;
t.MM = stamp / 60 / 24 / 31;
return t;
}
bool time_order(record a, record b)
{
return a.stamp < b.stamp;
}
int main()
{
int charge[24];
for (int i = 0; i < 24; i++)
{
cin >> charge[i];
}
int count;
cin >> count;
record records[count];
for (int i = 0; i < count; i++)
{
times t;
char c;
cin >> records[i].name >> t.MM >> c >> t.dd >> c >> t.HH >> c >> t.mm;
records[i].stamp = times2stamp(t);
string temp;
cin >> temp;
if (temp == "on-line")
records[i].status = 0;
else
records[i].status = 1;
}
sort(records, records + count, time_order);
set<string> names;
for (int i = 0; i < count; i++)
{
names.insert(records[i].name);
}
set<string>::iterator it;
for (it = names.begin(); it != names.end(); it++)
{
bool first = 1;
int total_money = 0;
int on = -1;
for (int i = 0; i < count; i++)
{
if (records[i].name == *it && records[i].status == 0)
on = i;
if (records[i].name == *it && records[i].status == 1 && on != -1)
{
times t1, t2;
t1 = stamp2times(records[on].stamp);
t2 = stamp2times(records[i].stamp);
int money = 0;
for (int j = records[on].stamp; j < records[i].stamp;)
{
times t = stamp2times(j);
if (t.mm == 0 && j + 59 < records[i].stamp)
{
money += charge[t.HH] * 60;
j += 60;
}
else
{
money += charge[t.HH];
j++;
}
}
if (first)
{
cout << *it << " ";
printf("%02d\n", stamp2times(records[0].stamp).MM);
first = 0;
}
printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n", t1.dd, t1.HH, t1.mm, t2.dd, t2.HH, t2.mm, records[i].stamp - records[on].stamp, money * 1.0 / 100);
total_money += money;
on = -1;
}
}
if (total_money != 0)
printf("Total amount: $%.2f\n", total_money * 1.0 / 100);
}
}