坑点1: 如果该用户的消费不满足有效条件 那么他的总消费也不必输出
坑点2:最后一组样例输出总消费时 应该考虑最后一组样例是否满足有效条件
有效条件: 第i个记录与第i+1条记录的名字相同并且第i个记录的状态是 "on-line" 第i+1个记录的状态是"off-line";
kswl
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 const int maxn = 0x3f3f3f3f; 7 typedef long long ll; 8 int bill[25]; 9 struct bl{ 10 string name; 11 int mon,day,hour,minu; 12 string st;// state on-line or off-line 13 }b[1005]; 14 string a = "on-line"; 15 string c = "off-line"; 16 bool cmp(bl p,bl q) {//排序部分 17 if(p.name != q.name) return p.name < q.name; 18 else if(p.mon != q.mon) return p.mon < q.mon; 19 else if(p.day != q.day) return p.day < q.day; 20 else if(p.hour != q.hour) return p.hour < q.hour; 21 return p.minu < q.minu; 22 } 23 int main() 24 { 25 for(int i = 0; i < 24; i++) { 26 cin >> bill[i]; 27 } 28 int n; 29 cin >> n; 30 for(int i = 0; i < n ; i++) { 31 cin >> b[i].name; 32 scanf("%d:%d:%d:%d",&b[i].mon,&b[i].day,&b[i].hour,&b[i].minu); 33 cin >> b[i].st; 34 } 35 sort(b,b+n,cmp); 36 int t = 0; 37 double total = 0; 38 bool flag = false; 39 for(int i = 0; i < n-1; i++) { 40 double money = 0; 41 if(b[i].name == b[i+1].name && b[i].st == a && b[i+1].st == c) { 42 int sum = b[i+1].day*60*24 + b[i+1].hour*60 + b[i+1].minu - (b[i].day*60*24 + b[i].hour*60 + b[i].minu); 43 for(int p = b[i].day; p <= b[i+1].day; p++) {//一分一分的算消费 44 int j = 0,jj = 23; 45 if(p == b[i].day) j = b[i].hour; 46 if(p == b[i+1].day) jj = b[i+1].hour; 47 for(; j <= jj; j++) { 48 int k = 0,kk = 60; 49 if(j == b[i].hour&&p == b[i].day) k = b[i].minu; 50 if(j == b[i+1].hour&&p == b[i+1].day) kk = b[i+1].minu; 51 for(;k < kk; k++) { 52 money += bill[j]; 53 } 54 } 55 } 56 money = money*1.0/100.0; 57 total += money; 58 if(t == 0) { 59 cout << b[i].name << " "; 60 if(b[i].mon < 10) cout << 0; 61 cout << b[i].mon << endl; 62 t = 1; 63 } 64 printf("%02d:%02d:%02d %02d:%02d:%02d ",b[i].day,b[i].hour,b[i].minu,b[i+1].day,b[i+1].hour,b[i+1].minu); 65 printf("%d $%.2lf\n",sum,money); 66 } else if(b[i].name != b[i+1].name) { 67 t = 0; 68 if(total != 0){//坑点1:当total不等于0时同时也说明该用户的消费满足有效条件 69 printf("Total amount: $%.2lf\n",total); 70 } 71 total = 0; 72 } 73 } 74 if(t == 1)//坑点2:最后一组也要判断是否满足有效条件 75 printf("Total amount: $%.2lf\n",total); 76 return 0; 77 }