[排序] PAT 1016 Phone Bills 笔记

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <queue>
#include <stack>
#include <math.h>
#include <cstring>
#include <map>
#include <cctype>
#define maxm 1010
#define inf 0x3fffffff
using namespace std;

struct Node{
    string id;
    int mh,d,h,m;
    bool flag;
}node[maxm];
int charge[25];
bool cmp(Node a,Node b){
    if(a.id!=b.id)return a.id<b.id;
    else if(a.d!=b.d)return a.d<b.d;
    else if(a.h!=b.h)return a.h<b.h;
    else return a.m<b.m;
}
int calmin(Node a,Node b){
    return b.d*24*60+b.h*60+b.m-a.d*24*60-a.h*60-a.m;
}

double calmon(Node a,Node b){
    int d=a.d,h=a.h,m=a.m;
    int total=0;
    while(d<b.d||h<b.h||m<b.m){
        total+=charge[h];
        m++;
        if(m==60){
            h++;m=0;
        }
        if(h==24){
            d++;h=0;
        }
    }
    return total/100.0;
}

int main() {
    for(int i=0;i<24;i++){
        cin>>charge[i];
    }
    int n;
    string stemp;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>node[i].id;
        scanf("%d:%d:%d:%d",&node[i].mh,&node[i].d,&node[i].h,&node[i].m);
        cin>>stemp;
        if(stemp=="on-line")node[i].flag=true;
        else node[i].flag=false;
    }
    sort(node,node+n,cmp);
    double total=0;
    int r=0,p=0,next;
    bool flag=false;  //while比for好写多了。。。。逻辑也更清晰
    while(r<n){
        next=p+1;
        stemp=node[r].id;
        while(next<n&&node[next].id==stemp){
            if(node[p].flag==true&&node[next].flag==false){
                if(flag==false){
                    cout<<node[p].id;
                    printf(" %02d\n",node[p].mh);
                    flag=true;
                }
                printf("%02d:%02d:%02d ",node[p].d,node[p].h,node[p].m);
                printf("%02d:%02d:%02d ",node[next].d,node[next].h,node[next].m);
                printf("%d $%0.2f\n",calmin(node[p],node[next]),calmon(node[p],node[next]));
                total+=calmon(node[p],node[next]);
            }
            p++;next++;
        }
        if(flag==true){
            printf("Total amount: $%0.2f\n",total);
        }
        flag=false;
        r=p=next;
        total=0;
    }

//    bool flag2=false,flag3=false;       //使用for实现的版本,条件判断比较复杂
//    for(int i=0;i<n-1;i++){
//        if(node[i].id==node[i+1].id){
//            if(node[i].flag==true&&node[i+1].flag==false){
//                if(flag2==false){
//                    cout<<node[i].id;
//                    printf(" %02d\n",node[i].mh);
//                    flag2=true;
//                    flag3=true;
//                }
//                printf("%02d:%02d:%02d ",node[i].d,node[i].h,node[i].m);
//                printf("%02d:%02d:%02d ",node[i+1].d,node[i+1].h,node[i+1].m);
//                printf("%d $%0.2f\n",calmin(node[i],node[i+1]),calmon(node[i],node[i+1]));
//                total+=calmon(node[i],node[i+1]);
//           }
//        }else if(flag2==true){
//            flag2=false;
//            printf("Total amount: $%0.2f\n",total);
//            total=0;
//            flag3=false;
//        }
//        if(i==n-2&&flag3==true){
//            printf("Total amount: $%0.2f\n",total);
//        }
//    }
    return 0;
}

这种题一开始感觉比较复杂,多看看打打就没什么了

### PAT 甲级 真题 1172 解析 对于PAT甲级真题1172,该题目名为“Phone Bill”,主要考察字符串处理以及简单的数据结构应用能力。此题目的背景设定为客户通话记录统计问题。 #### 题目描述 给定一组电话号码及其对应的拨打时间和持续时间,计算每位用户的月账单总额。每条通话记录包含三个字段:电话号码、起始时间和结束时间。要求按照输入顺序输出每个客户的总费用,并保留两位小数[^1]。 #### 输入格式说明 - 第一行给出正整数N (≤10^5),表示有N次呼叫; - 接下来N行,每行提供一次呼叫的信息:“手机号码 起始时刻 结束时刻”。其中,“起始时刻”和“结束时刻”的格式均为HH:MM:SS; #### 输出格式说明 - 对于每一个客户,先打印其手机号码,再跟上冒号和空格,最后是当月话费金额(精确到分),单位为元人民币RMB。 #### 示例代码实现 ```cpp #include <iostream> #include <map> #include <iomanip> using namespace std; int main() { int n; cin >> n; map<string, double> bills; while(n--) { string number; char start_time[9], end_time[9]; scanf("%s %s %s", &number[0], start_time, end_time); // Convert time strings to seconds since midnight. sscanf(start_time, "%*d:%*d:%d", &start_seconds); sscanf(end_time, "%*d:%*d:%d", &end_seconds); // Calculate duration and update bill accordingly. int duration = end_seconds - start_seconds; if(duration > 0){ bills[number] += ceil((double)duration / 60 * 0.01); } } for(auto& entry : bills){ cout << entry.first << ": " << fixed << setprecision(2) << entry.second << endl; } return 0; } ``` 上述C++程序实现了对输入数据的读取与处理逻辑,通过`<map>`容器来存储并累加各个用户的通话时长及相应费用。需要注意的是,在实际比赛中应当更加严谨地验证输入的有效性和边界条件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值