qq_51801942 2021-05-19 17:31 采纳率: 0%

pat的phone bill 一小时做题十小时找bug我快疯了有没有哪位大神救救孩子

#include<bits/stdc++.h>
using namespace std;
map<string,set<int> > on;
map<string,set<int> > off;
int month;
int wholeday=0;
int money[24];
int calculatemoney(int a,int b){
int sumnumber=0;
int day1=a/(24*60);
int day2=b/(24*60);
int hour1=a%(24*60)/60;
int hour2=b%(24*60)/60;
int minute1=a%(24*60)%60;
int minute2=b%(24*60)%60;
if(day1==day2)
{
for(int i=hour1;i<=hour2;i++)
sumnumber+=60*money[i];
sumnumber=sumnumber-minute1*money[hour1]-(60-minute2)*money[hour2];
}
if(day1<day2)
{int day=day2-day1-1;
sumnumber+=wholeday*day;
for(int i=hour1;i<24;i++)
sumnumber+=60*money[i];
for(int i=0;i<hour2;i++)
sumnumber+=60*money[i];
sumnumber-=money[hour1]*minute1;
sumnumber+=money[hour2]*minute2;

}
return sumnumber;
}

int main()
{

for(int i=0;i<24;i++)

{
cin>>money[i];
wholeday+=money[i]*60;
}
int number;
cin>>number;

for(int i=0;i<number;i++)
{string name;
int m;
int d;
int h;
int min;
cin>>name;

scanf("%d:%d:%d:%d",&m,&d,&h,&min);
month=m;
string of;
cin>>of;
if(of=="on-line")
on[name].insert(24*60*d+60*h+min);
else
off[name].insert(24*60*d+60*h+min);
}
for(map<string,set<int> >::iterator index1=on.begin();index1!=on.end();index1++)
{double summoney=0;
map<string,set<int> >::iterator index2;
index2=off.find(index1->first);

set<int>::iterator set1=index1->second.begin();
set<int>::iterator set2;
if(index2!=off.end())
set2=index2->second.begin();
else continue;
int truenumber=0;
while(set1!=index1->second.end()&&set2!=index2->second.end())

{
if(*set1>*set2)
{
while(*set1>*set2&&set1!=index1->second.end()&&set2!=index2->second.end())
set2++;
if(set2==index2->second.end())
{
break;
}

truenumber++;
if(truenumber==1)
{ cout<<index1->first<<" ";
printf("%02d\n",month);
}

printf("%02d:%02d:%02d %02d:%02d:%02d %d ",*set1/(24*60),*set1%(24*60)/60,*set1%(24*60)%60,*set2/(24*60),*set2%(24*60)/60,*set2%(24*60)%60,*set2-*set1);
double temp=calculatemoney(*set1,*set2);
summoney+=temp/100.0;
printf("\$%.2f\n",temp/100.0);
set2++;
set1++;

}
else
{
while(*set1<*set2&&set1!=index1->second.end()&&set2!=index2->second.end())
set1++;
set1--;

{truenumber++;
if(truenumber==1)
{ cout<<index1->first<<" ";
printf("%02d\n",month);
}
}

printf("%02d:%02d:%02d %02d:%02d:%02d %d ",*set1/(24*60),*set1%(24*60)/60,*set1%(24*60)%60,*set2/(24*60),*set2%(24*60)/60,*set2%(24*60)%60,*set2-*set1);
double temp=calculatemoney(*set1,*set2);
summoney+=temp/100.0;
printf("\$%.2f\n",temp/100.0);
set2++;
set1++;

}
}
if(truenumber>0)
printf("Total amount: \$%.2f\n",summoney);
}return 0;
}

• 写回答

1条回答默认 最新

• czx33859066 2024-05-13 14:25
关注

1、代码可读性太差了，中间有比较复杂的逻辑，要加点注释之类的说明
2、变量使用也有问题，用局部变量
3、truenumber++，这个是不是也没有正确使用

评论