zoukankan      html  css  js  c++  java
  • PAT 1016 Phone Bills

    map的使用,i.first,i.second 

    int a[23],声明0-22的数组,作为局部变量初始化a[23] ={0},a[24] 未知,全局变量都初始化为0;

    格式化输出 %02d 按两位数输出

    读入数据形式 aa:bb:cc:dd, scanf("%d:%d:%d:%d");

    统计费用差,将所有时间转换成同一单位分钟,便于大小的判断

    #include<iostream>
    #include<vector>
    #include<string>
    #include<algorithm>
    #include<map>
    using namespace std;
    int n;
    struct Node{
        string name;
        int time,state,month,day,hour,minute;
    };
    bool  cmp(Node &a,Node &b){
        return a.name!=b.name?a.name<b.name:a.time<b.time; 
    }
    
    double cal(Node a,int *rate){
        double ttt =0.0;
        ttt = a.day*rate[24]*60+rate[a.hour]*a.minute;
        for(int i=0;i<a.hour;i++){
            ttt+=rate[i]*60;
        } 
        ttt/=100.0;
        return ttt;
    }
    int main()
    {
        int rate[25]={0};
        for(int i=0;i<24;i++){
            cin>>rate[i];
            rate[24] += rate[i];
        }
        cin>>n;
        vector<Node>record(n+1);
        for(int i=1;i<=n;i++){
            string na;
            cin>>na;
            record[i].name = na;
            scanf("%d:%d:%d:%d",&record[i].month,&record[i].day,&record[i].hour,&record[i].minute);
            string state;
            cin>>state;
            record[i].state =(state == "on-line")?1:0;
            record[i].time = record[i].day*24*60+record[i].hour*60+record[i].minute;
        }
        sort(record.begin(),record.end(),cmp);
        map<string,vector<Node> >ans;
    
        for(int i=1;i<=n;i++){
            if(record[i].name == record[i-1].name &&record[i-1].state ==1&&record[i].state == 0)
            {
                ans[record[i-1].name].push_back(record[i-1]);
                ans[record[i].name].push_back(record[i]);
            }
        } 
        for(auto i:ans){
            vector<Node> it = i.second;
            double tot = 0.0;
            printf("%s %02d
    ",i.first.c_str(),it[0].month);
            for(int j=1;j<it.size();j+=2){
                //printf("
    cssss:%02d:%03d
    ",it[j].);
                double t =cal(it[j],rate)-cal(it[j-1],rate);
                printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f
    ",it[j-1].day,it[j-1].hour,it[j-1].minute,it[j].day,it[j].hour,it[j].minute,it[j].time-it[j-1].time,t);
                tot+=t;
            }
            printf("Total amount: $%.2f
    ",tot);
        }
        return 0;
    }
  • 相关阅读:
    vim 常用操作
    Zookeeper 一种简单的原子操作机制:
    用习惯的vimrc配置,在这里记录一下,以后可以继续完善使用
    static_cast, dynamic_cast, const_cast探讨【转】
    常用CSS标签使用
    Java基础
    Hibernate的第一个程序
    Hibernate的优缺点
    python基础语法
    ansible-role安装nginx,keepalived,tomcat
  • 原文地址:https://www.cnblogs.com/zxzmnh/p/11979219.html
Copyright © 2011-2022 走看看