Bo Master Welcome reprint, but please give the article link, I respect you, you respect me, thank you ~
Http://www.cnblogs.com/chenxiwenruo/p/6789229.html
Especially do not like those who reprint other people's original articles and do not give the link
So don't secretly copy blogger's blog Oh ~ ~
Give a 24-hour, hourly, per-minute call fee
Give N Records, on-line represents the start of a call, off-line represents the end of a call
If the on-line/off-line does not correspond to another, ignore it
First by the name of the sort, the same sort of time, where the time unified by minutes, that is, 24 * 60 Minutes a day, then 01:01:05 is 0*24*60+1*60+5
Then find each other paired with on-line and off-line, and deposit the phone array
Then the next step is to find out the cost per talk time
(mainly how to calculate the cost comparison details trouble, recommend to do)
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#defineONLINE 0#defineOFFLINE 1using namespacestd;Const intmaxn= ++5;Const intDayminute= -* -;Const intHourminute= -;inttoll[ -];intN;structrecord{Charname[ -]; intTime ; intmonth; intDay ; inthour; intminute; intMark; BOOL operator< (ConstRecord tmp)Const{ if(strcmp (name,tmp.name) = =0) returntime<Tmp.time; Else if(strcmp (Name,tmp.name) <0) return true; Else return false; }}RECORD[MAXN];structphone{Charname[ -]; intmonth; intD1,h1,m1; intd2,h2,m2; inttime1,time2;} PHONE[MAXN];intCnt=0;/** Find out the call time fee for the I pair*/intCalinti) { intsum=0; intstart=phone[i].time1;//Start Time intends=phone[i].time2;//End Time inttmp=0;//count the minutes of the call within the time period of each hour intIdx//the index of the toll for the corresponding time period for(intt=start;t<=ends;t++){ if(t% -==0){ if(t%dayminute==0) IDX= (dayminute-1)/ -;//if the module is 0, it should be dayminute. Elseidx= (t%dayminute-1)/ -;//because there may be consecutive calls for several days, so you have to take a modelSUM+=TMP*TOLL[IDX];//minutes of the call * fees for the time periodtmp=1;//For the sake of convenience, like 01:06:00 this whole time, counted as the next hour of } Elsetmp++; } if(TMP) {//For example 1:08:03, because 1:08:00 time counted as 8-9 between, actually statistics of tmp=4, so to 1sum+= (tmp-1) *toll[(Ends%dayminute)/ -]; } returnsum;}intMain () {Charword[Ten]; for(intI=0;i< -; i++) scanf ("%d",&Toll[i]); scanf ("%d",&N); for(intI=0; i<n;i++) {scanf ("%s%d:%d:%d:%d%s",record[i].name,&record[i].month,&record[i].day,&record[i].hour,&Record[i].minute,word); Record[i].time= (record[i].day-1) *dayminute+record[i].hour*hourminute+Record[i].minute; if(word[1]=='N') Record[i].mark=ONLINE; ElseRecord[i].mark=OFFLINE; } sort (Record,record+N); intlast=-1;//an index of the last on-line//find paired on-line and off-line, save to phone array for post-processing for(intI=0; i<n;i++){ if(record[i].mark==ONLINE) { Last=i; } if(Record[i].mark==offline && last!=-1){ if(strcmp (record[i].name,record[last].name) = =0) {strcpy (phone[cnt].name,record[i].name); Phone[cnt].month=Record[i].month; Phone[cnt].d1=Record[last].day; PHONE[CNT].H1=Record[last].hour; PHONE[CNT].M1=Record[last].minute; Phone[cnt].time1=Record[last].time; Phone[cnt].d2=Record[i].day; PHONE[CNT].H2=Record[i].hour; PHONE[CNT].M2=Record[i].minute; Phone[cnt].time2=Record[i].time; CNT++; Last=-1; } } } inttot=0, sum=0; for(intI=0; i<cnt;i++){ if(i==0) {printf ("%s%02d\n", Phone[i].name,phone[i].month); Sum=cal (i); Tot+=sum; intlen=phone[i].time2-phone[i].time1; Doublecost=sum*1.0/ -; printf ("%02d:%02d:%02d%02d:%02d:%02d%d $%.2lf\n", phone[i].d1,phone[i].h1,phone[i].m1,phone[i].d2,phone[i].h2,phone[i].m2,len,sum*1.0/ -); } Else{ if(strcmp (phone[i].name,phone[i-1].name)! =0) {printf ("Total Amount: $%.2lf\n", tot*1.0/ -); printf ("%s%02d\n", Phone[i].name,phone[i].month); Tot=0; } Sum=cal (i); Tot+=sum; intlen=phone[i].time2-phone[i].time1; intcost=sum*1.0/ -; printf ("%02d:%02d:%02d%02d:%02d:%02d%d $%.2lf\n", phone[i].d1,phone[i].h1,phone[i].m1,phone[i].d2,phone[i].h2,phone[i].m2,len,sum*1.0/ -); } } if(tot) {printf ("Total Amount: $%.2lf\n", tot*1.0/ -); } return 0;}
View Code
Pat a problem-1016. Phone Bills (25)-Analog, sort