浙大PAT甲级-1016

Phone Bills

1.更改文件输入输出方式:

freopen("input.txt","r",stdin);

freopen("output.txt","w",stdout);

******//中间代码正常cin,cout

fclose(stdin);

fclose(stdout);

2.vector删除某一项:

删除v[i]:

v.erase(v.begin()+i);

【注】:删除某项后其后面的项会自动向前补上,所以要是在遍历v时删除一项记得要将标记回退一项(i--)

3.C++字符串比较函数compare:

s1.compare(s2);

返回正数:s1>s2;

返回零:s1=s2;

返回负数:s1<s2.


#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int cents[24],n;

struct Record{
    string name;
    int month;
    int day;
    int hour;
    int minute;
    string status;
};
bool cmp(Record r1, Record r2){
    if(r1.name.compare(r2.name)==0){
        if(r1.day<r2.day)
            return 1;
        else if(r1.day>r2.day)
            return 0;
        else{
            if(r1.hour<r2.hour)
                return 1;
            else if(r1.hour>r2.hour)
                return 0;
            else{
                if(r1.minute<r2.minute)
                    return 1;
                else
                    return 0;
            }
        }
    }else if(r1.name.compare(r2.name)>0){
        return 0;
    }else{
        return 1;
    }
}
int tim(Record r1,Record r2){//r1到r2时间差
    return r2.day*24*60+r2.hour*60+r2.minute-r1.day*24*60-r1.hour*60-r1.minute;
}
int callAllDay(){//打了一整天
    int t=0;
    for(int i=0;i<24;i++){
        t+=cents[i]*60;
    }
    return t;
}
int callNtoM(int n,int m){//从n点整打到m点整
    int t=0;
    for(int i=n;i<m;i++){
        t+=cents[i]*60;
    }
    return t;
}
double mone(Record r1,Record r2){//r1到r2的话费
    int total=0;
    if(r1.day<r2.day){
        int i=r2.day-r1.day;
        total+=(i-1)*callAllDay();
        total+=callNtoM(r1.hour+1,24);
        total+=(60-r1.minute)*cents[r1.hour];

        total+=callNtoM(0,r2.hour);
        total+=r2.minute*cents[r2.hour];
    }else{
        if(r2.hour>r1.hour){
            total+=callNtoM(r1.hour+1,r2.hour);
            total+=(60-r1.minute)*cents[r1.hour]+r2.minute*cents[r2.hour];
        }else{
            total+=(r2.minute-r1.minute)*cents[r1.hour];
        }
    }
    return total/100.0;
}
int main()
{
    int i;
    vector<Record> r;
    //freopen("input.txt","r",stdin);//重置输入流,文件输入
    for(i=0;i<24;i++)
        cin>>cents[i];

    cin>>n;
    for(i=0;i<n;i++){
        string n,s;
        int mo,d,h,mi;
        cin>>n;
        scanf("%d:%d:%d:%d",&mo,&d,&h,&mi);
        cin>>s;
        Record re;
        re.name=n;
        re.day=d;
        re.hour=h;
        re.minute=mi;
        re.month=mo;
        re.status=s;
        r.push_back(re);
    }
    sort(r.begin(),r.end(),cmp);//根据用户名和时间排序

    string sn=r[0].name;
    bool valid=0;

    for(i=0;i<r.size();i++){//删除无效记录
        if(sn!=r[i].name){
            if(valid==1){
                r.erase(r.begin()+i-1);
                i--;
            }
            sn=r[i].name;
            valid=0;
        }
        if(valid==0&&r[i].status=="on-line"){
            valid=1;
        }else if(valid==0&&r[i].status=="off-line"){
            r.erase(r.begin()+i);
            i--;//删除vector元素后,其后面的元素会向前补上,所以i要减一
        }else if(valid==1&&r[i].status=="off-line"){
            valid=0;
        }else if(valid==1&&r[i].status=="on-line"){
            r.erase(r.begin()+i-1);
            i--;
        }
    }
    if(valid==1)
        r.erase(r.begin()+i-1);

    sn=r[0].name;
    cout<<sn<<" ";
    printf("%02d",r[0].month);
    cout<<endl;
    double total=0.0;
    for(i=0;i<r.size();i++){//打印输出
        if(r[i].name!=sn){
            cout<<"Total amount: $";
            printf("%.2f",total);
            cout<<endl;
            sn=r[i].name;
            cout<<sn<<" ";
            printf("%02d",r[i].month);
            cout<<endl;
            total=0.0;
        }
        if(r[i].status=="on-line"){
            printf("%02d:%02d:%02d",r[i].day,r[i].hour,r[i].minute);
            cout<<" ";
        }
        if(r[i].status=="off-line"){
            printf("%02d:%02d:%02d",r[i].day,r[i].hour,r[i].minute);
            int time=tim(r[i-1],r[i]);
            double money=mone(r[i-1],r[i]);
            cout<<" "<<time<<" $";
            printf("%.2f",money);
            cout<<endl;
            total+=money;
        }
    }
    cout<<"Total amount: $";
    printf("%.2f",total);
    //fclose(stdin);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值