1: 计算时间的函数参考的
http://blog.csdn.net/zhu_liangwei/article/details/10360533
2:qsort函数第三个参数:
qsort(records, n, sizeof(Record), cmp);//using sizeof(records[0]) is also fine
3:注意在遍历records时,因为是个数组,记录下on-line 的下标就行了,别蠢萌地定义个指针!
int onPos;//last on-line record
4:少了一个$导致全错,长点心吧!
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <list> using namespace std; int p[24];//price/minute typedef struct{ char name[21]; int month; int d; int h; int m; int type;//on or off }Record; int cmp(const void * a, const void * b){ Record * r1 = (Record *)a; Record * r2 = (Record *)b; if(strcmp(r1->name, r2->name) != 0){ return strcmp(r1->name, r2->name); }else{ if(r1->d != r2->d){ return (r1->d -r2->d); }else if(r1->h != r2->h){ return (r1->h -r2->h); }else{ return (r1->m -r2->m); } } } long calculateMinDiff(Record a, Record b, double * amount){//calculate for each pair of call int d1 = a.d, d2 = b.d; int h1 = a.h, h2 = b.h; int m1 = a.m, m2 = b.m; long minCount = 0; double res = 0; while(m1 < m2 || h1 < h2 || d1 < d2){//so fucking clever method minCount++; res += p[h1]; m1++; if(m1 == 60){ m1 = 0; h1++; if(h1 == 24){ h1 = 0; d1++; } } } *amount = res/100; return minCount; } int main(){ //freopen("in.txt","r",stdin); int i, n; Record * records; for(i = 0; i<24; i++){ scanf("%d",&p[i]); } scanf("%d",&n); records = (Record *)malloc(n*sizeof(Record)); for(i = 0; i < n; i++){ Record r; scanf("%s %d:%d:%d:%d",r.name, &r.month, &r.d,&r.h,&r.m); char tmp[10]; scanf("%s",tmp); if(strcmp(tmp,"on-line") == 0){ r.type = 0; }else{ r.type = 1; } records[i] = r; } qsort(records, n, sizeof(Record), cmp);//using sizeof(records[0]) is also fine //test /*for(i = 0; i < n; i++){ Record r = records[i]; printf("%s %d:%d:%d:%d %d\n",r.name, r.month, r.d,r.h,r.m, r.type); }*/ char currentName[21]; int flag = 0;// 0 needs on, 1 needs off long curMinSum = 0; double curAmountSum = 0; int onPos;//last on-line record int firstPair = 0;//when 1st pair found for a customer, print custoemr name and month first strcpy(currentName, records[0].name);//first customer for(i = 0; i < n; i++){ Record cur = records[i]; if(strcmp(cur.name, currentName) != 0 ){// start processing a new customer if(curAmountSum > 0){ printf("Total amount: $%.2lf\n", curAmountSum); } curMinSum = 0; curAmountSum = 0; flag = 0; strcpy(currentName, cur.name); firstPair = 0; } if( (cur.type == 1 && flag == 0)){//off off continue; } if((cur.type == 0)){//got the latest on /*validOn = &cur;*/ //can't point to cur, because cur is a variable which has changed to point at current record onPos = i; flag = 1;//needs off }else {//cur == 1 and flag == 1 Record on = records[onPos]; double amount = 0; long minDiff = calculateMinDiff(on, cur, &amount); if(minDiff > 0){ if(firstPair == 0){ printf("%s %02d\n", cur.name, cur.month); firstPair = 1; } printf("%02d:%02d:%02d %02d:%02d:%02d %ld $%.2lf\n",on.d,on.h,on.m, cur.d,cur.h,cur.m, minDiff,amount); curMinSum += minDiff; curAmountSum += amount; } flag = 0; } } if(curAmountSum > 0){ //last customer: printf("Total amount: $%.2lf", curAmountSum); } free(records); records = NULL; return 0; }