直接把时间化成以秒为单位 排序 匹配 计算就好。
#include <iostream>#include <string>#include <vector>#include <set>#include <algorithm>using namespace std;struct Node { string name; string time; string state;};int char2int(char i) { int result = 0; switch (i) { case '1': result = 1; break; case '2': result = 2; break; case '3': result = 3; break; case '4': result = 4; break; case '5': result = 5; break; case '6': result = 6; break; case '7': result = 7; break; case '8': result = 8; break; case '9': result = 9; break; case '0': result = 0; break; default: break; } return result;}int str2day(string s) { int day = 0; day = char2int(s[3]) * 10 + char2int(s[4]); return day;}int str2hour(string s) { int hour = 0; hour = char2int(s[6]) * 10 + char2int(s[7]); return hour;}int str2minute(string s) { int minute = 0; minute = char2int(s[9]) * 10 + char2int(s[10]); return minute;}int CalRate(string time1, string time2, vector <int> rate) {//M1 < M2 int D1, D2, H1, H2, M1, M2; D1 = str2day(time1); D2 = str2day(time2); H1 = str2hour(time1); H2 = str2hour(time2); M1 = str2minute(time1); M2 = str2minute(time2); int Sum = 0; if (D1 == D2) { //同一天 if (H1 == H2) {//同一小时 Sum += (M2 - M1)* rate[H1]; return Sum; } else{//不同小时 Sum += (60 - M1) * rate[H1]; Sum += M2 * rate[H2]; for (int i = H1+1; i < H2; i++) Sum += 60 * rate[i]; return Sum; } } else{ //不同天 Sum += (60 - M1)* rate[H1]; for (int i = H1 + 1; i < 24; i++) { Sum += 60 * rate[i]; } Sum += M2 * rate[H2]; for (int i = 0; i < H2; i++) { Sum += 60 * rate[i]; } for (int i = D1 + 1; i < D2; i++) { for (int j = 0; j < 24; j++) { Sum += rate[j] * 60; } } return Sum; } }void PRintTime(string s) { for (int i = 3; i <= 10; i++) { cout << s[i]; }}void TotalRate(vector <Node> list ,vector <int> rate) { //对一个用户的账单进行生成 bool tag = false;//是否匹配 bool Head = true;//又没有显示Head bool HaveBill = false; float SumRate = 0; int time = 0; Node pre; //匹配前 //cout << list[0].name << " " << list[0].time[0] << list[0].time[1] << endl; for (int i = 0; i < list.size(); i++) { if (!tag && list[i].state == "off-line")//前面没有on 却有off 忽略 continue; else if (!tag && list[i].state == "on-line") {//前面没有on 后面weion pre等于该数 tag = true; pre = list[i]; } else if (tag && list[i].state == "on-line") { //前面有on 后面还有on 更新pre pre = list[i]; } else {//匹配成功 if(Head){ cout << list[i].name << " " << list[i].time[0] << list[i].time[1] << endl; Head = false; HaveBill = true; } tag = false; int PreTime = str2day(pre.time) * 24 * 60 + str2hour(pre.time) * 60 + str2minute(pre.time); int Time = str2day(list[i].time) * 24 * 60 + str2hour(list[i].time) * 60 + str2minute(list[i].time); int gapTime = Time - PreTime; float tempRate = CalRate(pre.time, list[i].time, rate); float tR = tempRate / 100; SumRate += tR; PrintTime(pre.time); cout << " "; PrintTime(list[i].time); cout << " " << gapTime << " $"; printf("%.02f", tR); cout << endl; } } if (HaveBill) { cout << "Total amount: $"; printf("%.2f", SumRate); cout << endl; }}bool cmp(Node N1 ,Node N2) { if (N1.name < N2.name) return true; else if (N1.name == N2.name && str2day(N1.time) < str2day(N2.time)) return true; else if (N1.name == N2.name && str2day(N1.time) == str2day(N2.time) && str2hour(N1.time) < str2hour(N2.time)) return true; else if (N1.name == N2.name && str2day(N1.time) == str2day(N2.time) && str2hour(N1.time) == str2hour(N2.time) && str2minute(N1.time) < str2minute(N2.time)) return true; else return false;}int main() { vector <int> rate; int tempRate; for (int i = 0; i < 24; i++) { cin >> tempRate; rate.push_back(tempRate); } int N;//记录数 cin >> N; Node * record = new Node[N]; set<string> r; for (int i = 0; i < N; i++) { cin >> record[i].name >> record[i].time >> record[i].state; r.insert(record[i].name); } sort(record, record + N, cmp);//对记录进行排序 vector <Node> * List = new vector<Node>[r.size()]; //对不同用户进行分组 string temp = record[0].name; int tempi = 0; for (int i = 0; i < N; i++) {//分组 if (temp == record[i].name) List[tempi].push_back(record[i]); else { tempi++; temp = record[i].name; List[tempi].push_back(record[i]); } } //for (int i = 0; i < r.size(); i++) { // for (int j = 0; j < List[i].size(); j++) // cout << List[i][j].name << " " << List[i][j].time << " " << List[i][j].state << endl; //} for (int i = 0; i < r.size(); i++) { TotalRate(List[i], rate); } return 0; }
新闻热点
疑难解答