第一次用map STL,其中map<string,int>
,最好不要用char*做map的key,详见stackoverflows上的讨论
http://stackoverflow.com/questions/4157687/using-char-as-a-key-in-stdmap/4157811#4157811
要注意如果一个账户没有满足条件的账单,则它什么都不输出(名字也不输出)
#include<stdio.h>
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<string>
#define SIZE 1000
using namespace std;
struct person{
string name;
int month, day, hour, min;
bool online;
};
int rate[24];
bool cmp(const struct person &a, const struct person &b){
if (a.month == b.month)
if (a.day == b.day)
if (a.hour == b.hour)
return a.min < b.min;
else return a.hour < b.hour;
else return a.day < b.day;
else return a.month < b.month;
}
vector<struct person> custom;
map<string, vector<struct person>> cus;
map<string, vector<struct person>>::iterator it;
void PrintNum(int n){
if (n < 10)
printf("0%d", n);
else printf("%d", n);
}
void PrintTime(const struct person &a){
PrintNum(a.day);
putchar(':');
PrintNum(a.hour);
putchar(':');
PrintNum(a.min);
}
double Cost(struct person &a,struct person &b){
double fee = 0;
int minute = 0;
while (a.day != b.day ||a.hour != b.hour||a.min != b.min){
fee += rate[a.hour];
minute++;
a.min++;
if (a.min >= 60)
{
a.min = 0;
a.hour++;
}
if (a.hour >= 24){
a.hour = 0;
a.day++;
}
}
printf("%d ", minute);
return fee/100;
}
int main(){
freopen("1.in", "r", stdin);
int NumOfBill, NumOfPerson = 0;
int i;
for (i = 0; i < 24; i++)
scanf("%d", &rate[i]);
scanf("%d", &NumOfBill);
string name,state;
int day, month, hour, min;
struct person node;
int index;
for (i = 0; i < NumOfBill; i++){
cin >> name;
scanf("%d:%d:%d:%d",&month, &day, &hour, &min);
cin >> state;
node.name = name;
node.month = month;
node.day = day;
node.hour = hour;
node.min = min;
if (state == "on-line")
node.online = true;
else node.online = false;
cus[name].push_back(node);
}
//sort(cus.begin(), cus.end());
//printf("%d\n", cus.size());
vector<struct person>::iterator k;
for (it = cus.begin(); it != cus.end(); it++){
custom = (*it).second;
sort(custom.begin(), custom.end(), cmp);
k = custom.begin();
while (k != custom.end()){
if ((*k).online)
break;
k++;
}
if (k == custom.end()||k+1 == custom.end())
continue;
double totalfee = 0,fee = 0;
struct person begin, end;
bool flag = false;
bool nameflag = false;
for (; k != custom.end(); k++){
if ((*k).online){
begin = *k;
flag = true;
}
else if (flag &&!(*k).online){
if (nameflag == false){
cout << (*k).name;
putchar(' ');
PrintNum((*k).month);
putchar('\n');
nameflag = true;
}
end = *k;
PrintTime(begin);
putchar(' ');
PrintTime(end);
putchar(' ');
fee = Cost(begin, end);
totalfee += fee;
printf("$%.2lf\n", fee);
flag = false;
}
else continue;
}
if (nameflag)
printf("Total amount: $%.2lf\n", totalfee);
}
return 0;
}