pat 1016

第一次用map STL,其中map<string,int>,最好不要用char*做map的key,详见stackoverflows上的讨论
http://stackoverflow.com/questions/4157687/using-char-as-a-key-in-stdmap/4157811#4157811

要注意如果一个账户没有满足条件的账单,则它什么都不输出(名字也不输出)

#include<stdio.h>
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<string>
#define SIZE 1000
using namespace std;

struct person{
    string name;
    int month, day, hour, min;
    bool online;
};

int rate[24];

bool cmp(const struct person &a, const struct person &b){
    if (a.month == b.month)
        if (a.day == b.day)
            if (a.hour == b.hour)
                return a.min < b.min;
            else return a.hour < b.hour;
        else return a.day < b.day;
    else return a.month < b.month;
}

vector<struct person> custom;
map<string, vector<struct person>> cus;
map<string, vector<struct person>>::iterator it;

void PrintNum(int n){
    if (n < 10)
        printf("0%d", n);
    else printf("%d", n);
}

void PrintTime(const struct person &a){
    PrintNum(a.day);
    putchar(':');
    PrintNum(a.hour);
    putchar(':');
    PrintNum(a.min);
}

double Cost(struct person &a,struct person &b){
    double fee = 0;
    int minute = 0;
    while (a.day != b.day ||a.hour != b.hour||a.min != b.min){
        fee += rate[a.hour];
        minute++;
        a.min++;
        if (a.min >= 60)
        {
            a.min = 0;
            a.hour++;
        }
        if (a.hour >= 24){
            a.hour = 0;
            a.day++;
        }
    }
    printf("%d ", minute);
    return fee/100;
}
int main(){
    freopen("1.in", "r", stdin);
    int NumOfBill, NumOfPerson = 0;
    int i;
    for (i = 0; i < 24; i++)
        scanf("%d", &rate[i]);
    scanf("%d", &NumOfBill);
    string name,state;
    int day, month, hour, min;
    struct person node;
    int index;
    for (i = 0; i < NumOfBill; i++){
        cin >> name;
        scanf("%d:%d:%d:%d",&month, &day, &hour, &min);
        cin >> state;
        node.name = name;
        node.month = month;
        node.day = day;
        node.hour = hour;
        node.min = min;
        if (state == "on-line")
            node.online = true;
        else node.online = false;
        cus[name].push_back(node);
    }
    //sort(cus.begin(), cus.end());
    //printf("%d\n", cus.size());
    vector<struct person>::iterator k;
    for (it = cus.begin(); it != cus.end(); it++){
        custom = (*it).second;
        sort(custom.begin(), custom.end(), cmp);
        k = custom.begin();
        while (k != custom.end()){
            if ((*k).online)
                break;
            k++;
        }
        if (k == custom.end()||k+1 == custom.end())
            continue;
        double totalfee = 0,fee = 0;
        struct person begin, end;
        bool flag = false;
        bool nameflag = false;
        for (; k != custom.end(); k++){
            if ((*k).online){
                begin = *k;
                flag = true;
            }
            else if (flag &&!(*k).online){
                if (nameflag == false){
                    cout << (*k).name;
                    putchar(' ');
                    PrintNum((*k).month);
                    putchar('\n');
                    nameflag = true;
                }
                end = *k;
                PrintTime(begin);
                putchar(' ');
                PrintTime(end);
                putchar(' ');
                fee = Cost(begin, end);
                totalfee += fee;
                printf("$%.2lf\n", fee);
                flag = false;
            }
            else continue;
        }
        if (nameflag)
            printf("Total amount: $%.2lf\n", totalfee);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值