PAT.A1016 Phone Bills

返回目录在这里插入图片描述

题意

输入24小时,每个小时的电话费多少,再输入n个电话记录,格式为(用户名 月份:日期:小时:分钟 on-line/off-line)
按照字典序输出存在有用收费的用户记录,输出格式为:
用户名 月份
online1 offline1 分钟数1 $话费1
online2 offline2 分钟数2 $话费2
Total amount: $总话费

注意点

  1. 本题大致思路为先对记录进行排序,再按照排序好的记录,找到用户的记录起点i和终点next(下一个记录的起点),再在i-next的记录里输出用户的资费信息
  2. 排序的cmp写法:按照用户的字典序(strcmp(a.name,b.name)),再按照时间的先后顺序比较
  3. 找到next的记录过程中,用needprint标记当前用户是否有on-line和off-line连在一起的记录,如果有needprint值会变成2,否则不用输出
  4. print()函数用来输出相邻两个有效记录的时间差和money,除了本题使用循环的方式计算外,还可以使用以分为单位的作差法,但由于每个小时的资费不同,计算起来较为麻烦
  5. 输出钱时要将cent转换成dollar,所以要除以100
#include <bits/stdc++.h>
using namespace std;

struct record{
	char name[25];
	int mm,dd,hh,ss;//月份:日期:小时:分钟
	int on;//0表示离线,1表示上线
}rec[1010],a,b;
int cost[24];
bool cmp(record a,record b){
	int s=strcmp(a.name,b.name);
	if(s!=0)return s<0;
    if(a.mm!=b.mm)return a.mm<b.mm;
	if(a.dd!=b.dd)return a.dd<b.dd;
	if(a.hh!=b.hh)return a.hh<b.hh;
	return a.ss<b.ss;
}
void print(int on,int& money){
	a=rec[on];b=rec[on+1];
    int time=0;
	while(a.dd<b.dd||a.hh<b.hh||a.ss<b.ss){
		time++;
		money+=cost[a.hh];
		a.ss++;
		if(a.ss>=60){
			a.ss=0;
			a.hh++;
		}
		if(a.hh>=24){
			a.hh=0;
			a.dd++;
		}
	}
    printf("%d $%.2f\n",time,money/100.0);
}

int main(){
	for(int i=0;i<24;i++)scanf("%d",&cost[i]);
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		char line[10];
		scanf("%s %d:%d:%d:%d %s",rec[i].name,&rec[i].mm,&rec[i].dd,&rec[i].hh,&rec[i].ss,line);
		if(strcmp(line,"on-line")==0)rec[i].on=1;
		else rec[i].on=0;
	}
	sort(rec,rec+n,cmp);
	int i=0,next;
	while(i<n){
		int needprint=0;
        next=i;
        while(next<n&&strcmp(rec[i].name,rec[next].name)==0){
            if(needprint==0&&rec[next].on==1)needprint=1;
            if(needprint==1&&rec[next].on==0)needprint=2;
            next++;
        }
        if(needprint<2){
            i=next;continue;
        }
        printf("%s %02d\n",rec[i].name,rec[i].mm);
        int allmoney=0;
        while(i<next){
            while(i<next-1&&rec[i].on-rec[i+1].on!=1)i++;
            if(i+1==next){
                i=next;
                break;
            }
            printf("%02d:%02d:%02d ",rec[i].dd,rec[i].hh,rec[i].ss);
            printf("%02d:%02d:%02d ",rec[i+1].dd,rec[i+1].hh,rec[i+1].ss);
            int money=0;
            print(i,money);
            allmoney+=money;
            i++;
        }
        //Total amount: $638.80
        printf("Total amount: $%.2f\n",allmoney/100.0);
	}	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小怪兽会微笑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值