本题完全按照别人的思路来写。之前在如何处理输入数据上想了很久,发现还是不能处理好,于是参考了别人的思路。我好菜...
1.对于输入的数据,先按照姓名排序,如果名字相同按照时间排序(时间转化为分钟)。分别比较相邻两个元素的名字是否相同,再比较on-line和off-line的标志是否相异,即可判断是否为有效数据。
2.计算某一时刻到另一时刻的费用时比较繁琐,可以先计算出从0时刻分别到两个时刻的花费,然后相减即可。
3.c++中的string很好用,c的输出格式控制很方便,而且可以节省输入输出时间,出于以上考虑,混合了c和c++的输入输出方式。
1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<vector> 5 #include<map> 6 #include<algorithm> 7 using namespace std; 8 double rate[25]={0}; 9 struct CALL{ 10 string name; 11 int mon,dd,hh,mm; 12 int status; 13 int time; 14 }; 15 int toMin(CALL call){//将时间转化为距离月初的时间 16 return call.dd*24*60+call.hh*60+call.mm; 17 } 18 bool cmp(CALL a,CALL b){ 19 if(a.name!=b.name) 20 return a.name<b.name; 21 else 22 return a.time<b.time; 23 } 24 double getCost(CALL x){ 25 double ans=0; 26 ans=ans+rate[x.hh]*x.mm;//分钟 27 for(int i=0;i<x.hh;i++){//小时 28 ans=ans+rate[i]*60; 29 } 30 ans=ans+rate[24]*60*x.dd; 31 return ans; 32 } 33 double Cost(CALL x,CALL y){ 34 return (getCost(x)-getCost(y)); 35 } 36 int main(){ 37 38 for(int i=0;i<24;i++){ 39 cin>>rate[i]; 40 rate[24]+=rate[i];//小技巧 41 } 42 int N; 43 int mon,dd,hh,mm; 44 string name,onoff; 45 cin>>N; 46 47 vector<CALL> A(N); 48 for(int i=0;i<N;i++){ 49 cin>>A[i].name; 50 scanf("%d:%d:%d:%d",&A[i].mon,&A[i].dd,&A[i].hh,&A[i].mm); 51 cin>>onoff; 52 if(onoff=="on-line") 53 A[i].status=1; 54 else 55 A[i].status=0; 56 A[i].time=toMin(A[i]); 57 } 58 sort(A.begin(),A.end(),cmp); 59 double Total=0; 60 map<string,vector<CALL> > B; 61 for(int i=1;i<N;i++){ 62 if(A[i].name==A[i-1].name&&A[i].status==0&&A[i-1].status==1){ 63 B[A[i].name].push_back(A[i-1]); 64 B[A[i].name].push_back(A[i]); 65 } 66 } 67 for(map<string,vector<CALL> >::iterator it=B.begin();it!=B.end();it++){ 68 cout<<it->first<<" "; 69 printf("%02d\n",A[0].mon); 70 vector<CALL> temp=it->second; 71 double total=0; 72 for(int i=0;i<temp.size();i++){ 73 printf("%02d:%02d:%02d ",temp[i].dd,temp[i].hh,temp[i].mm); 74 if(i%2!=0) 75 { printf("%d ",temp[i].time-temp[i-1].time); 76 total+=Cost(temp[i],temp[i-1]); 77 printf("$%.2f\n",Cost(temp[i],temp[i-1])/100) ; 78 } 79 } 80 printf("%s%.2f\n","Total amount: $",total/100); 81 82 } 83 84 return 0; 85 }
参考链接:https://blog.csdn.net/qq_34594236/article/details/78576273
原文:https://www.cnblogs.com/wsshub/p/12501886.html