算是学到了模拟时间了
while(start.day < end.day
|| start.hour < end.hour
|| start.minute < end.minute){
// 每分钟做的事
// 模拟时间
start.minute++;
if(temp.time.minute >= 60){
start.minute = 0;
start.hour++;
}
if(temp.time.hour >= 24){
start.hour=0;
start.day++;
}
}
#include<bits/stdc++.h>
using namespace std;
double rate[24];
int N;
struct Time{
int month;
int day;
int hour;
int minute;
bool operator<(Time & b){ // 时间按照优先级为 月、日、时、分的顺序排序
if(month == b.month) {
if(day == b.day) {
if(hour == b.hour){
return minute < b.minute;
}
return hour < b.hour;
}
return day < b.day;
}
return month < b.month;
}
};
struct Bill{
string name;
Time time;
string state;
bool operator<(Bill &b){
if(name == b.name)
return time < b.time;
return name < b.name;
}
}bills[1001];
struct Lists{
Time start;
Time end;
int minu;
double fee;
};
map<string, vector<Lists> > L; // 创建邻接表存储每一个人的每一次通话和每一次的费用,就不用费心去控制了
void getans(int on,int off, int & ti, double & money){ // 求每个用户的每次的通话时间和费用
Bill temp = bills[on];
// 从通话开始一分钟一分钟地模拟到通话结束
while(temp.time.day < bills[off].time.day
|| temp.time.hour < bills[off].time.hour
|| temp.time.minute < bills[off].time.minute)
{
ti++;
money += rate[temp.time.hour];
// 模拟时间
temp.time.minute++;
if(temp.time.minute >= 60){
temp.time.minute = 0;
temp.time.hour++;
}
if(temp.time.hour >= 24){
temp.time.hour=0;
temp.time.day++;
}
}
}
int main() {
double onedayFee = 0.0;
for(int i = 0;i <24;++i){
int r;
cin >> r;
rate[i] = r*0.01;
}
cin >> N;
for(int i = 0;i < N;++i){
cin >> bills[i].name;
scanf("%d:%d:%d:%d ", &bills[i].time.month, &bills[i].time.day, &bills[i].time.hour, &bills[i].time.minute);
cin >> bills[i].state;
}
sort(bills, bills+N);
for(int i = 0;i < N-1;){
if(bills[i].name == bills[i+1].name && bills[i].state == "on-line" && bills[i+1].state == "off-line") {
Time start = bills[i].time, end = bills[i+1].time;
//int totalMinute = ( (end.day - start.day)*24 + end.hour - start.hour)*60 + end.minute - start.minute;
int totalMinute = 0;
double totalFee = 0.0;
getans(i, i+1, totalMinute, totalFee);
L[bills[i].name].push_back({start, end, totalMinute,totalFee}); // 存储
i+=2;
} else
i++;
}
for(auto i: L){
cout << i.first << " ";
printf("%02d\n", i.second[0].start.month);
double totalAmount = 0.0;
for(auto j: i.second){
printf("%02d:%02d:%02d %02d:%02d:%02d ", j.start.day, j.start.hour, j.start.minute, j.end.day, j.end.hour, j.end.minute);
printf("%d $%.2lf\n", j.minu, j.fee);
totalAmount += j.fee;
}
printf("Total amount: $%.2lf\n", totalAmount);
}
return 0;
}