引入地址让函数内可以改变全局变量。
利用next来标记具体人物
然后on在next 内移动,寻找配对的on和off输出
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef struct node
{
char name[100];
int MM;
int dd;
int hh;
int mm;
bool status;
}record;
int toll[24];
record R[10000];
bool cmp(record a,record b)
{
int s = strcmp(a.name,b.name);
if(s!=0)
return s<0;
else if(a.MM !=b.MM)
return a.MM<b.MM;
else if(a.dd != b.dd)
return a.dd<b.dd;
else if(a.hh != b.hh)
return a.hh<b.hh;
else if(a.mm != b.mm)
return a.mm<b.mm;
}
void getans(int on,int off,int &time,int &money)
{
record temp = R[on];
while(temp.dd < R[off].dd || temp.hh < R[off].hh || temp.mm < R[off].mm)
{
time ++;
money += toll[temp.hh];
temp.mm ++;
if(temp.mm >=60)
{
temp.mm = 0;
temp.hh++;
}
if(temp.hh >=24)
{
temp.hh =0;
temp.dd++;
}
}
}
int main(int argc, char** argv) {
int n=0;
char Sta[1000];
int i =0,flag = 0;
int P[100] = {0};
int index =0;
int j =0;
for(int i =0;i<24;i++)
{
scanf("%d",&toll[i]);
}
scanf("%d",&n);
for(i = 0;i<n;i++)
{
scanf("%s",R[i].name);
scanf("%d:%d:%d:%d",&R[i].MM,&R[i].dd,&R[i].hh,&R[i].mm);
scanf("%s",Sta);
if(strcmp(Sta,"on-line") == 0)
{
R[i].status = true;
}
else
R[i].status = false;
}
sort(R,R+n,cmp);
int on = 0,next,off;
while(on<n)
{
int needp = 0;
next = on;
while(next <n &&strcmp(R[on].name,R[next].name) == 0)
{
if(R[next].status == true && needp ==0)
needp = 1;
else if(R[next].status == false && needp ==1)
needp = 2;
next ++;
}
if(needp != 2)
{
on = next;
continue;
}
int All = 0;
printf("%s %02d\n",R[on].name,R[on].MM);
while(on<next)
{
while(on<next - 1 &&
!(R[on].status == true && R[on +1].status == false))
{
on ++;
}
off = on +1;
if(off == next)
{
on = next;
break;
}
printf("%02d:%02d:%02d ",R[on].dd,R[on].hh,R[on].mm);
printf("%02d:%02d:%02d ",R[off].dd,R[off].hh,R[off].mm);
int time=0,money =0;
getans(on,off,time,money);
printf("%d $%.2f\n",time,money/100.0);
All +=money;
on = off +1;
}
printf("Total amount: $%.2f\n",All/100.0) ;
}
return 0;
}
在这里插入代码片
题目以后还得再做一遍,自己水平太低,看了好几遍算法笔记才写出来。