1016 Phone Bills (25 分)
思路
这道题代码量可不少了,虽然没用到什么算法。
计算时间差,将两个时间戳转换成多少秒,再做差;
计算花费,写了个循环,计算钱,比那种一秒一秒算钱的快不少。
strcmp strcpy这些都要用的熟练才行~
最难的是比较前后是否匹配了,还有if-else真的别乱加,得细细斟酌,就是else多加一句导致由两个测试点不能通过,debug了半天,PAT测试点2、3都是大量数据,大概50条以上这样子。可以通过牛客网的题目测试点帮助检查。
牛客网链接🔗
代码
#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);
}