写bug 40分钟,debug 20分钟。。。。这还是在我已经看了别人思路,构建好自己的代码以后的样子。。。我好难
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
struct Dates {
string name, time, statue;
};
struct Print1 {
string a, b, na;
int d; float mo;
};
bool cmp(Dates &A,Dates &B) {
return A.name == B.name ? A.time < B.time : A.name < B.name;
}
int main() {
int rate[24];
map<string, float>M;
map<string, int>flag;
vector<Print1>V2;
float daymoney = 0;
for (int i = 0; i < 24; i++) { scanf("%d", &rate[i]); daymoney += 60 * rate[i]; }
int N;
scanf("%d", &N);
vector<Dates>V;
string time;
Dates tmp;
int month1;
for (int i = 0; i < N; i++) {
cin.ignore();
cin >> tmp.name >> time >> tmp.statue;
month1 = stoi(time.substr(0, 2));
tmp.time = time.substr(3, 8);
V.push_back(tmp);
}
sort(V.begin(), V.end(), cmp);
int i = 0, j = 1, day1, hour1, minute1, minute2, day2, hour2, allminutes = 0;
float money = 0;
Print1 P;
while (j <= V.size() - 1) {
if (V[i].name == V[j].name&&V[i].statue == "on-line"&&V[j].statue == "off-line") {
day1 = stoi(V[i].time.substr(0, 2));
day2 = stoi(V[j].time.substr(0, 2));
minute1 = stoi(V[i].time.substr(6, 2));
minute2 = stoi(V[j].time.substr(6, 2));
hour1 = stoi(V[i].time.substr(3, 2));
hour2 = stoi(V[j].time.substr(3, 2));
if (day1 == day2) {
if (hour1 == hour2) { money += (minute2 - minute1)*rate[hour1]; allminutes += minute2 - minute1; }
else
for (int i = hour1; i <= hour2; i++) {
if (i == hour1) { money += (60 - minute1)*rate[hour1]; allminutes += 60 - minute1; }
else if (i == hour2) { money += minute2 * rate[hour2]; allminutes += minute2; }
else {
money += 60 * rate[i]; allminutes += 60;
}
}
}
else {
money+=(day2 - day1 - 1)*daymoney;
allminutes += (day2 - day1 - 1) * 24*60;
for (int i = hour1; i <= 23; i++) {
if (i == hour1) { money += (60 - minute1)*rate[hour1]; allminutes += (60 - minute1); }
else { money += 60 * rate[i]; allminutes += 60; }
}
for (int i = 0; i <= hour2; i++) {
if (i == hour2) {
money += minute2 * rate[i]; allminutes += minute2;
}
else { money += 60 * rate[i]; allminutes += 60; }
}
}
M[V[i].name] += money;
P.a = V[i].time; P.b = V[j].time; P.d = allminutes; P.mo = money; P.na = V[i].name;
V2.push_back(P);
money = 0;
allminutes = 0;
}
i++; j++;
}
for (int i = 0; i < V2.size(); i++) {
if (flag[V2[i].na] == 0) {
printf("%s %02d\n", V2[i].na.c_str(), month1); flag[V2[i].na]++;
}
printf("%s %s %d $%.2f\n", V2[i].a.c_str(), V2[i].b.c_str(), V2[i].d, V2[i].mo / 100);
if ((i<V2.size()-1&& V2[i].na != V2[i + 1].na) || i == V2.size() - 1)printf("Total amount: $%.2f\n", M[V2[i].na]/100);
}
return 0;
}
这个最后一个测试点考察 同一天,同一小时,即:
输入:
10 10 10 10 10 10 20 20 20 15 15 15 15 15 15 15 20 30 20 15 15 10 10 10
2
CYLL 01:01:06:01 on-line
CYLL 01:01:06:05 off-line
输出:
CYLL 01
01:06:01 01:06:05 4 $0.80
Total amount: $0.80