題目不難,但輸入輸出數據多,格式多,需要些許思維和注意。花費了非常多時間,也算有些收獲。首先,卡在輸入輸出上,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代碼: [cpp] //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); } www.2cto.com 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; }