这个题研究好久……蠢哭了
直接贴代码
代码
#include <cstdio>
#include <algorithm>
#include <string.h>
using namespace std;
const int maxn=1010;
int rate[25];
struct Rec{
char name[30];
int month;
int day;
int hour;
int minu;
int state; //state=1 means"on-line";2means"off-line" 用数字更方便
}rec[maxn],temp;
bool cmp(Rec a,Rec b){
if (strcmp(a.name,b.name)!=0) return strcmp(a.name,b.name)<0;
else{
if(a.day!=b.day) return a.day<b.day;
else if(a.hour!=b.hour) return a.hour<b.hour;
else return a.minu<b.minu;
}
}
void ans(int on, int off,int &time,int &money){ //计算time和money,传入指针
temp=rec[on];
while(temp.day<rec[off].day || temp.hour<rec[off].hour || temp.minu<rec[off].minu){
time++;
money+=rate[temp.hour];
temp.minu++;
if(temp.minu>=60){
temp.minu=0;
temp.hour++;
}
if(temp.hour>=24){
temp.hour=0;
temp.day++;
}
}
}
int main(){
int n;
for(int i=0;i<24;i++){
scanf("%d",&rate[i]);
}
scanf("%d",&n);
char word[10]; //用来临时存状态
for(int i=0;i<n;i++){
scanf("%s%d:%d:%d:%d%s",rec[i].name,&rec[i].month,&rec[i].day,&rec[i].hour,&rec[i].minu,word);
if(strcmp(word,"on-line")==0){
rec[i].state=1;
}else{
rec[i].state=2;
}
}
sort(rec,rec+n,cmp);
int on=0,off,next;
while(on<n){
int needprint=0; //1表示需要输出
next=on;
while(next<n&&strcmp(rec[next].name,rec[on].name)==0){ //同一个用户的数据处理
if(needprint==0 && rec[next].state == 1){
needprint=1; //找到第一个on-line
}else {
if(needprint==1 && rec[next].state ==2){
needprint=2; //找到与上面的on-line匹配的off
}
}
next++;
}
if(needprint<2){
on=next; //若<2,则on=下一个记录,且不执行下面代码进入下个循环
continue;
}
int total=0; //即needprint>2的情况,执行下面代码
printf("%s %02d\n",rec[on].name,rec[on].month);
while(on<next){
while(on<next-1 && !(rec[on].state==1&&rec[on+1].state==2)){
on++; //on-off未配对,on自增1;特殊情况on=next-1(下面写) }
off=on+1;
if(off==next){ //特殊情况,off=next,这条是下一个用户的记录
on=next;
break;
}
printf("%02d:%02d:%02d ",rec[on].day,rec[on].hour,rec[on].minu);
printf("%02d:%02d:%02d ",rec[off].day,rec[off].hour,rec[off].minu);
int time=0,money=0;
ans(on,off,time,money);
total+=money;
printf("%d $%.2f\n",time,money/100.0);
on=off+1;
}
printf("Total amount: $%.2f\n",total/100.0);
}
return 0;
}