不是AC代码,4个样例只过了第一个,这题太麻烦了,有时间再写,先写后边的,次品代码先存档在这。
题意:计算月花费账单。
每个样例都会在某个月内。
给出一天24小时的收费标准,单位cents/minute。算出每个用户的月账单。
对每个on-line记录,找时间上最近的off-line记录匹配。
struct customer{
vector<string> online;
vector<string> offline;
};
struct ans{
string ss;
string ee;
int minuu;
double money;
};
bool cmp(ans a1,ans a2){
string ss1 = a1.ss;
string ss2 = a2.ss;
string ss3 = a1.ee;
string ss4 = a2.ee;
bool bb;
if(strcmp(ss1.c_str(),ss2.c_str())>0) bb = false;
else if(strcmp(ss1.c_str(),ss2.c_str())<0) bb = true;
else if(strcmp(ss1.c_str(),ss2.c_str())==0){
if(strcmp(ss3.c_str(),ss4.c_str())>0) bb = true;
if(strcmp(ss3.c_str(),ss4.c_str())<0) bb = false;
}
return bb;
}
int costs[24];
map<string,customer> cmap;
set<string> good;//利用set排序的特点
int get_month(string time){
int mouth = (time[0]-'0')*10 + (time[1]-'0');
return mouth;
}
//转换为分钟数
int min_ver(string time){
int date = (time[3]-'0')*10 + (time[4]-'0');
int hour = (time[6]-'0')*10 + (time[7]-'0');
int minu = (time[9]-'0')*10 + (time[10]-'0');
return date*24*60 + hour*60 + minu;
}
int get_cost(string s,string e){
int c = 0,daycost=0;
for(int i=0;i<24;i++){
daycost += 60*costs[i];
}
int d1 = (s[3]-'0')*10 + s[4]-'0';
int h1 = (s[6]-'0')*10 + s[7]-'0';
int m1 = (s[9]-'0')*10 + s[10]-'0';
int d2 = (e[3]-'0')*10 + e[4]-'0';
int h2 = (e[6]-'0')*10 + e[7]-'0';
int m2 = (e[9]-'0')*10 + e[10]-'0';
if(d1==d2){
c = (60-m1)*costs[h1] + m2*costs[h2];
for(int i=h1+1;i<h2;i++)
c += costs[i]*60;
}else{
for(int i=h1+1;i<24;i++){
c += 60*costs[i];
}
c += costs[h1]*(60-m1);
for(int i=0;i<h2;i++){
c += 60*costs[i];
}
c += costs[h2]*m2;
c += daycost*(d2-d1-1);
}
return c;
}
void process(string name){
int month = get_month(cmap[name].online[0]);
double cnt = 0;
printf("%s %02d\n",name.c_str(),month);
vector<ans> anss;
while(cmap[name].online.size() && cmap[name].offline.size()){
//当online和offline还有能配对的记录时,就给他们配对
//二重循环进行配对
int minn = INT_MAX,temp,flag=0,i,j,si,ej;
string startt,endd;
for(i=0;i<cmap[name].online.size();i++)
{
for(j=0;j<cmap[name].offline.size();j++)
{
temp = min_ver(cmap[name].offline[j]) - min_ver(cmap[name].online[i]);
if(temp>0 && temp<minn){
flag = 1;
minn = temp;
startt = cmap[name].online[i];
endd = cmap[name].offline[j];
si = i;
ej = j;
}
}
}
if(flag){
//去掉计算过的记录
cmap[name].online.erase(cmap[name].online.begin()+si);
cmap[name].offline.erase(cmap[name].offline.begin()+ej);
double costt = get_cost(startt,endd)/100.0;
startt = startt.substr(3);//去掉日期
endd = endd.substr(3);
//printf("%s %s %d $%.02f\n",startt.c_str(),endd.c_str(),minn,costt);
ans a;
a.ss = startt;
a.ee = endd;
a.minuu = minn;
a.money = costt;
anss.push_back(a);
cnt += costt;
}else break;
}
//将通话记录按时间排序
sort(anss.begin(),anss.end(),cmp);
for(int i=0;i<anss.size();i++)
printf("%s %s %d $%.02f\n",anss[i].ss.c_str(),anss[i].ee.c_str(),anss[i].minuu,anss[i].money);
printf("Total amount: $%.02lf\n",cnt);
}
int main()
{
int n;
for(int i=0;i<24;i++) scanf("%d",costs+i);
scanf("%d",&n);
getchar();//需要吃掉回车
while(n--){
string str;
getline(cin,str);
string as,bs,cs;
stringstream ss;
ss<<str;
ss>>as;
ss>>bs;
ss>>cs;
good.insert(as);//借用set自动排序
if(!cs.compare("on-line")) cmap[as].online.push_back(bs);
if(!cs.compare("off-line")) cmap[as].offline.push_back(bs);
}
for(set<string>::iterator it=good.begin();it!=good.end();++it)
process(*it);
return 0;
}