题目不难,但输入输出数据多,格式多,需要些许思维和注意。花费了非常多时间,也算有些收获。首先,卡在输入输出上,string类型只能用cin,不能用scanf,printf ,若要用printf,只能用printf("%s",str.c_str())。输入中若带“:”,得用getchar()。在printf时,最后一个%02d少加了个%,一直报越界。要细心。此题发现,pat测试数据爱出在临界条件上,如01:02:14:58-01:02:14:59 ,若像我原本只考虑时间加头加尾加中间的,就会出错。另外,值得注意的点是,It is guaranteed that at least one call is well paired in the input. 只保证每次输入都至少有一次正确匹配,不保证每个用户都有一次正确匹配,因此可能出现有用户无一次正确匹配,此时不要输出用户的任何信息。我还是太弱了~
AC代码:
//1016 13:00 #include<iostream> #include<string> #include<cstdlib> using namespace std; const int NUM=1005; struct record { string name; int dd,hh,mm; string status; int total; }rec[NUM]; struct call { int s; int e; double m; }cal[NUM]; int toll[26]; int sumToll; int cmp(const void *a, const void *b) { record *x=(record *)a; record *y=(record *)b; // if(x->name==y->name&&x->dd==y->dd&&x->hh==y->hh) // return x->mm - y->mm; // if(x->name==y->name&&x->dd==y->dd) // return x->hh - y->hh; // if(x->name==y->name) // return x->dd - y->dd; // return x->name < y->name; if(x->name==y->name) return x->total - y->total; if(x->name < y->name) return -1; else return 1; } int find(int start,int end) { int ans=0,i; if(rec[start].dd<rec[end].dd){ for(i=rec[start].hh+1;i<24;i++){ ans+=toll[i]*60; } for(i=0;i<rec[end].hh;i++) ans+=toll[i]*60; ans+=(60-rec[start].mm)*toll[rec[start].hh]; ans+=rec[end].mm*toll[rec[end].hh]; ans+=(rec[end].dd-rec[start].dd-1)*sumToll*60; } else if(rec[start].dd==rec[end].dd) { if(rec[end].hh>rec[start].hh){ for(i=rec[start].hh+1;i<rec[end].hh;i++) ans+=toll[i]*60; ans+=(60-rec[start].mm)*toll[rec[start].hh]; ans+=rec[end].mm*toll[rec[end].hh]; } else { ans+=(rec[end].mm-rec[start].mm)*toll[rec[end].hh]; } } else ans=-1; return ans; } int main() { int n,i,j,month; freopen("C:\\Documents and Settings\\Administrator\\桌面\\input.txt","r",stdin); sumToll=0; for(i=0;i<24;i++){ cin>>toll[i]; sumToll+=toll[i]; } cin>>n; for(i=0;i<n;i++){//string类型只能用cin,不能用scanf,printf ,若要用printf,只能用printf("%s",str.c_str()) //cin>>rec[i].name>>month>>":">>rec[i].dd>>":">>rec[i].hh>>":">>rec[i].mm>>rec[i].status; cin>>rec[i].name; cin>>month; getchar(); cin>>rec[i].dd; getchar(); cin>>rec[i].hh; getchar(); cin>>rec[i].mm; cin>>rec[i].status; rec[i].total=rec[i].dd*60*24+rec[i].hh*60+rec[i].mm; } qsort(rec,n,sizeof(rec[0]),cmp); string str=""; int start,end,flag=0; int theSame=0; rec[n].name=""; double money=0.0f; int minute; double sum=0.0f; int index=0; for(i=0;i<n;i++){ if(str!=rec[i].name){ str=rec[i].name; flag=0; theSame=0; index=0; } else theSame=1;//与前一个相同 if(rec[i].status[1]=='n'){//on start=i; flag=1;//1表示前面已经出现on了 } if(theSame==1&&flag==1&&rec[i].status[1]=='f'){//off 和前面是同一个 end=i; flag=0;//0 表示出现off了,结束了,可以重新开始 money=find(start,end); money=money/100.0; sum+=money; cal[index].s=start; cal[index].e=end; cal[index].m=money; //printf("%02d:%02d:%02d %02d:%02d:%02d %d $%0.2lf\n",rec[start].dd,rec[start].hh,rec[start].mm,rec[end].dd,rec[end].hh,rec[end].mm,minute,money); index++; } if(rec[i].name!=rec[i+1].name&&index!=0){ printf("%s %02d\n",rec[i].name.c_str(),month); for(j=0;j<index;j++){ start=cal[j].s; end=cal[j].e; money=cal[j].m; minute=rec[end].total-rec[start].total; printf("%02d:%02d:%02d %02d:%02d:%02d %d $%0.2lf\n",rec[start].dd,rec[start].hh,rec[start].mm,rec[end].dd,rec[end].hh,rec[end].mm,minute,money); } printf("Total amount: $%.2lf\n",sum); sum=0.0f; index=0; } } // printf("\n"); // for(i=0;i<n;i++){ // printf("%s %02d:%02d:%02d:%02d %s\n",rec[i].name.c_str(),month,rec[i].dd,rec[i].hh,rec[i].mm,rec[i].status.c_str()); // } return 0; }