A1016——Phone Bills

这个题研究好久……蠢哭了

直接贴代码

代码

#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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值