【PAT甲级】1016. Phone Bills (25)

注:如果一个用户所有通话记录均不匹配,则不打印他的任何信息

#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

typedef struct Rec {
    string name;
    int month;
    int d;
    int h;
    int m;
    string onoff;
} Rec;
typedef struct Out {
    string name;
    int month;
    int d1;
    int h1;
    int m1;
    int d2;
    int h2;
    int m2;
    int hours;
    float toll;
    public:
    Out(string n, int m, int a1, int b1, int c1, int a2, int b2, int c2) {
        name = n;
        month = m;
        d1 = a1;
        h1 = b1;
        m1 = c1;
        d2 = a2;
        h2 = b2;
        m2 = c2;
        hours = 0;
        toll = 0;
    }
} Out;
int rate[24];
vector<Rec> records;
vector<Out> output[1001];
int n;
int nameCount;
float totalToll[1001]; 

float putRecord(int nameNum, Rec a, Rec b);
void printTotal(int id);
bool Comp(Rec a, Rec b);

int main (int argc, char *argv[]) {
    int i, j, k;
    for (i = 0; i < 24; i++)
        scanf("%d", &rate[i]);
    scanf("%d", &n);
    if (n == 0) return 0;
    for (i = 0; i < n; i++) {
        Rec r;
        cin >> r.name;
        scanf("%d:%d:%d:%d", &r.month, &r.d, &r.h, &r.m);
        cin >> r.onoff;
        records.push_back(r);
    }
    sort(records.begin(), records.end(), Comp);
    nameCount = 0;
    memset(totalToll, 0.0, 1001 * sizeof(float)); 
    bool onlineFlag = false;
    if (records[0].onoff == "on-line")
        onlineFlag = true;
    for (i = 1; i < n; i++) {
        if (records[i].name == records[i - 1].name) {
            if (onlineFlag == false && records[i].onoff== "on-line") {
                onlineFlag = true;
            } else if (onlineFlag == false && records[i].onoff== "off-line") {
                continue;
            } else if (onlineFlag == true && records[i].onoff== "on-line") {
                continue;
            } else if (onlineFlag == true && records[i].onoff== "off-line") {
                float t = putRecord(nameCount, records[i - 1], records[i]);
                totalToll[nameCount] += t;
                onlineFlag = false;
            }
        } else {
            nameCount++;
            if (records[i].onoff == "on-line")
                onlineFlag = true;
            else
                onlineFlag = false;
        }
    }
    nameCount++;
    for (i = 0; i < nameCount; i++) {
        if (output[i].size() == 0)
            continue;
        cout << output[i][0].name << " ";
        printf("%02d\n", output[i][0].month);
        for (j = 0; j < output[i].size(); j++) {
            printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n", output[i][j].d1, output[i][j].h1, output[i][j].m1, output[i][j].d2, output[i][j].h2, output[i][j].m2, output[i][j].hours, output[i][j].toll); 
        }
        printTotal(i);
    }
}

float putRecord(int nameNum, Rec a, Rec b) {
    int tm = 0;//time
    int t = 0;//min oney - cents
    Out o(a.name, a.month, a.d, a.h, a.m, b.d, b.h, b.m);
    if (b.d > a.d) {
        b.h += (b.d - a.d) * 24;
    }
    if (a.h == b.h) {
        t = (b.m - a.m) * rate[a.h];
        tm = (b.m - a.m);
    } else {
        t = (60 - a.m) * rate[a.h];
        tm = 60 - a.m;
        for (int i = a.h + 1; i <= b.h; i++) {
            if (i < b.h) { 
                t = t + rate[i % 24] * 60;
                tm = tm + 60;
            } else {
                t = t + rate[i % 24] * b.m;
                tm = tm + b.m;
            }
        }
    }
    float tdollar = float(t) / 100.0;
    o.hours = tm;
    o.toll = tdollar;
    output[nameNum].push_back(o);
    return tdollar;
}
void printTotal(int id) {
    if (totalToll[id] != 0)
        printf("Total amount: $%.2f\n", totalToll[id]);
}

bool Comp(Rec a, Rec b) {
    if (a.name < b.name) return true;
    else if (a.name == b.name) {
        if (a.d < b.d) return true;
        else if (a.d == b.d) {
            if (a.h < b.h) return true;
            else if (a.h == b.h) {
                if (a.m < b.m) return true;
            }
        }
    }
    return false;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值