# PAT甲级-排序类型-1016 Phone Bills解题思路

### 思路

strcmp strcpy这些都要用的熟练才行～

### 代码

#include <bits/stdc++.h>
using namespace std;

struct stu
{
char id[25];
int month,dd,hh,mm;
bool status;
}stud[2005];

bool com(stu a,stu b)
{
int s =strcmp(a.id,b.id);
if(s != 0)
return s<0;
else if(a.month != b.month)return a.month<b.month;
else if(a.dd!=b.dd)return a.dd < b.dd;
else if(a.hh!=b.hh)return a.hh <b.hh;
else return a.mm<b.mm;
}
int num[25];

int compute_time(int d1,int d2,int h1,int h2,int  m1,int m2)
{
int t1 = 0 ,t2 = 0;
if(d1 == d2)
{
t1  = h1*60 +m1 ;
t2 = h2*60 +m2 ;
return t1 - t2;
}
else
{
int ttt = 24 * 60;
int d = d1 -d2;
t1  = h1*60 +m1 ;
t2 = h2*60 +m2 ;

int all_te = (d-1)*ttt + t1 -t2 +ttt;
return all_te;
}
}

int compute_money(int d2,int h2,int m2,int all_time)
{
int money = 0;
while(all_time>=0)
{
if((all_time + m2)>60)
{
money += num[h2]*(60-m2);
h2 = (h2+1)%24;
all_time = all_time -(60-m2);
m2 = 0;
}
else
{
money += num[h2]*(all_time);
return money;
}
}
return money ;
}

int main()
{

for(int i =0;i<24;i++)
cin>>num[i];

int N;

cin>>N;
char sta[20];
for(int i = 0;i<N;i++)
{
scanf("%s %d:%d:%d:%d %s",stud[i].id,&stud[i].month,&stud[i].dd,&stud[i].hh,&stud[i].mm,sta);
if(!strcmp(sta,"on-line") )
stud[i].status = 0;
else
stud[i].status = 1;
}

sort(stud,stud+N,com);

int flag = 0;
char start_name[25]="lukeda";
int start_status = -1;
double all_money = 0;
int d2,h2,m2;
for(int i=0;i<N;i++)
{
//cout<<stud[i].id<<" "<<stud[i].status<<endl;
if(stud[i].status == 0 )
{
if (!strcmp(start_name , stud[i].id))
{
strcpy(start_name,stud[i].id);
d2 = stud[i].dd;
h2 = stud[i].hh;
m2 = stud[i].mm;
start_status = 0;
continue;
}
else
{
strcpy(start_name,stud[i].id);
d2 = stud[i].dd;
h2 = stud[i].hh;
m2 = stud[i].mm;
if (all_money)
printf("Total amount: $%.2f\n",all_money); all_money = 0; start_status = 0; flag = 0; continue; } } else if(stud[i].status == 1) { if (!strcmp(start_name , stud[i].id) && start_status==0) { int all_time = compute_time(stud[i].dd ,d2 ,stud[i].hh,h2,stud[i].mm,m2); double money = compute_money(d2,h2,m2,all_time); money /=100; all_money += money; start_status = 1; flag += 1; if(flag==1) printf("%s %02d\n",stud[i].id,stud[i].month); printf("%02d:%02d:%02d %02d:%02d:%02d",d2,h2,m2,stud[i].dd,stud[i].hh,stud[i].mm); printf(" %d$%.2f\n",all_time,money);

}
else
{
start_status = 1;
}
}
}
if (all_money)
printf("Total amount: \$%.2f\n",all_money);
}



02-01 58

03-13 470
08-20 302
03-04 1932
11-19 333
02-26 539
01-31 1403
07-17 215
04-02 105
08-13 41
03-26 2431