1016. Phone Bills (25)

来源:互联网 发布:什么动物知天下事 编辑:程序博客网 时间:2024/06/05 02:34

1016. Phone Bills (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

A long-distance telephone company charges its customers by the following rules:

Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting a long-distance call, the time will be recorded, and so will be the time when the customer hangs up the phone. Every calendar month, a bill is sent to the customer for each minute called (at a rate determined by the time of day). Your job is to prepare the bills for each month, given a set of phone call records.

Input Specification:

Each input file contains one test case. Each case has two parts: the rate structure, and the phone call records.

The rate structure consists of a line with 24 non-negative integers denoting the toll (cents/minute) from 00:00 - 01:00, the toll from 01:00 - 02:00, and so on for each hour in the day.

The next line contains a positive number N (<= 1000), followed by N lines of records. Each phone call record consists of the name of the customer (string of up to 20 characters without space), the time and date (mm:dd:hh:mm), and the word "on-line" or "off-line".

For each test case, all dates will be within a single month. Each "on-line" record is paired with the chronologically next record for the same customer provided it is an "off-line" record. Any "on-line" records that are not paired with an "off-line" record are ignored, as are "off-line" records not paired with an "on-line" record. It is guaranteed that at least one call is well paired in the input. You may assume that no two records for the same customer have the same time. Times are recorded using a 24-hour clock.

Output Specification:

For each test case, you must print a phone bill for each customer.

Bills must be printed in alphabetical order of customers' names. For each customer, first print in a line the name of the customer and the month of the bill in the format shown by the sample. Then for each time period of a call, print in one line the beginning and ending time and date (dd:hh:mm), the lasting time (in minute) and the charge of the call. The calls must be listed in chronological order. Finally, print the total charge for the month in the format shown by the sample.

Sample Input:
10 10 10 10 10 10 20 20 20 15 15 15 15 15 15 15 20 30 20 15 15 10 10 1010CYLL 01:01:06:01 on-lineCYLL 01:28:16:05 off-lineCYJJ 01:01:07:00 off-lineCYLL 01:01:08:03 off-lineCYJJ 01:01:05:59 on-lineaaa 01:01:01:03 on-lineaaa 01:02:00:01 on-lineCYLL 01:28:15:41 on-lineaaa 01:05:02:24 on-lineaaa 01:04:23:59 off-line
Sample Output:
CYJJ 0101:05:59 01:07:00 61 $12.10Total amount: $12.10CYLL 0101:06:01 01:08:03 122 $24.4028:15:41 28:16:05 24 $3.85Total amount: $28.25aaa 0102:00:01 04:23:59 4318 $638.80Total amount: $638.80
分析:将给出的数据先按照姓名排序,再按照时间的先后顺序排列,这样遍历的时候,前后两个名字相同且前面的状态为on-line后面一个的状态为off-line的就是合格数据

(1)自己写的,好几个小时

/*这里处理二个时间距离很重要的一个思想就是: 1、找到一个便于计算的中间时间(这里是0天0时0分0秒)(我想到和给出二个时间日期,求之间的相隔天数的思路差不多),分别求是时间间隔T1、T2; T1-T2就是要求的二个时间之间的距离,求总费用同理;2、这里对每个客户产生的总费用summoney进行是否为零判断,如果为零不输出,否则测试点1,2无法通过3、开始是对客户名字的字母顺序进行排序,每个客户再按照时间前后进行排序 */ #include<iostream>#include<algorithm>#include<cstring>#include<iomanip>using namespace std;struct People{char name[25];char day[15];char statue[10];};People person[1001];int n;int cost[25];double summoney=0;char name[10]="000";//判断前后客户名字不同进行名字输出 bool oneperson(int from,int to);int cmp1(const People &a,const People &b){if(strcmp(a.name,b.name)!=0)return strcmp(a.name,b.name)<0;elsereturn strcmp(a.day,b.day)<0;}void init(){for(int i=1;i<=24;++i){cin>>cost[i];cost[0]+=cost[i];}cin>>n;for(int i=0;i<n;++i)cin>>person[i].name>>person[i].day>>person[i].statue;sort(person,person+n,cmp1);int i;for(i=0;i<n;i=i+2){if(i>=2){if(strcmp(person[i].name,person[i-1].name)!=0){if(summoney!=0)//判断不为零很重要 cout<<"Total amount: $"<<summoney<<endl;summoney=0; }}if(i+1>=n)break;if(oneperson(i,i+1)==false)i--;}if(summoney!=0)//判断总的账单不为零,测试点1,2通过 cout<<"Total amount: $"<<summoney<<endl;//最后一个客户的账单金额 //for(int i=0;i<n;++i)//cout<<person[i].name<<" "<<person[i].day<<" "<<person[i].statue<<endl;}int time(int d,int h,int m)//计算距离时间为0的时间距离 {int sum=d*24*60+h*60+m;return  sum;}double allmoney(int d,int h,int m)//计算总的金额 {double money=cost[0]*d*60;for(int i=1;i<=h;++i)money+=60*cost[i];money+=m*cost[h+1];money/=100;return money;}bool oneperson(int from,int to){char on[10]="on-line";char off[10]="off-line";int sum1=0,sum2=0;double money1=0,money2=0;if(strcmp(person[from].name,person[to].name)==0 && strcmp(person[from].statue,on)==0 && strcmp(person[to].statue,off)==0)//如果满足条件进行金额账单的计算 {if(strcmp(person[from].name,name)!=0){cout<<person[from].name<<" "<<person[from].day[0]<<person[from].day[1]<<endl;strcpy(name,person[from].name);}int dd1=10*(person[from].day[3]-'0')+person[from].day[4]-'0';int hh1=10*(person[from].day[6]-'0')+person[from].day[7]-'0';int mm1=10*(person[from].day[9]-'0')+person[from].day[10]-'0';int dd2=10*(person[to].day[3]-'0')+person[to].day[4]-'0';int hh2=10*(person[to].day[6]-'0')+person[to].day[7]-'0';int mm2=10*(person[to].day[9]-'0')+person[to].day[10]-'0';sum1=time(dd1,hh1,mm1);sum2=time(dd2,hh2,mm2);money1=allmoney(dd1,hh1,mm1);money2=allmoney(dd2,hh2,mm2);summoney+=money2-money1;for(int i=3;i<11;++i)cout<<person[from].day[i];cout<<" ";for(int i=3;i<11;++i)cout<<person[to].day[i];cout<<" "<<sum2-sum1;cout<<" "<<"$"<<fixed<<setprecision(2)<<money2-money1<<endl;return true;}elsereturn false;}int main(){freopen("in.txt","r",stdin);init();return 0; } 

(2)别人用了容器map和vector,大大的简化了代码,惭愧啊。粘贴学习一下。其实一些容器的简单函数使用还是要会的,简单就够用了

#include <iostream>#include <map>#include <vector>#include <algorithm>using namespace std;struct node {    string name;    int status, month, time, day, hour, minute;//将对应的数据进行数值化处理 };bool cmp(node a, node b) {    return a.name != b.name ? a.name < b.name : a.time < b.time;}double billFromZero(node call, int *rate) {    double total = rate[call.hour] * call.minute + rate[24] * 60 * call.day;    for (int i = 0; i < call.hour; i++)        total += rate[i] * 60;    return total / 100.0;}int main() {    int rate[25] = {0}, n;    for (int i = 0; i < 24; i++) {        scanf("%d", &rate[i]);        rate[24] += rate[i];    }    scanf("%d", &n);    vector<node> data(n);    for (int i = 0; i < n; i++) {        cin >> data[i].name;        scanf("%d:%d:%d:%d", &data[i].month, &data[i].day, &data[i].hour, &data[i].minute);        string temp;        cin >> temp;        data[i].status = (temp == "on-line") ? 1 : 0;        data[i].time = data[i].day * 24 * 60 + data[i].hour * 60 + data[i].minute;    }    sort(data.begin(), data.end(), cmp);    map<string, vector<node> > custom;//这里的映射很重要,重点学习     for (int i = 1; i < n; i++) {        if (data[i].name == data[i - 1].name && data[i - 1].status == 1 && data[i].status == 0) {            custom[data[i - 1].name].push_back(data[i - 1]);            custom[data[i].name].push_back(data[i]);        }    }    for (auto it : custom) {        vector<node> temp = it.second;        cout << it.first;        printf(" %02d\n", temp[0].month);        double total = 0.0;        for (int i = 1; i < temp.size(); i += 2) {            double t = billFromZero(temp[i], rate) - billFromZero(temp[i - 1], rate);            printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n", temp[i - 1].day, temp[i - 1].hour, temp[i - 1].minute, temp[i].day, temp[i].hour, temp[i].minute, temp[i].time - temp[i - 1].time, t);            total += t;        }        printf("Total amount: $%.2f\n", total);    }    return 0;}

(3)
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#define ONLINE 0#define OFFLINE 1using namespace std;const int maxn=1000+5;const int DAYMINUTE=24*60;const int HOURMINUTE=60;int toll[24];int n;struct Record{    char name[25];    int time;    int month;    int day;    int hour;    int minute;    int mark;    bool operator<(const Record tmp)const{        if(strcmp(name,tmp.name)==0)            return time<tmp.time;        else if(strcmp(name,tmp.name)<0)            return true;        else            return false;    }}record[maxn];struct Phone{    char name[25];    int month;    int d1,h1,m1;    int d2,h2,m2;    int time1,time2;}phone[maxn];int cnt=0;/**求出第i个配对的通话时间费用*/int cal(int i){    int sum=0;    int start=phone[i].time1; //起始时间    int ends=phone[i].time2;  //结束时间    int tmp=0; //统计每个小时的时间段内,通话的分钟    int idx; //对应时间段toll的索引    for(int t=start;t<=ends;t++){        if(t%60==0){            if(t%DAYMINUTE==0)                idx=(DAYMINUTE-1)/60;  //如果模为0,就应该是DAYMINUTE            else                idx=(t%DAYMINUTE-1)/60; //因为可能会有连续通话了好几天,所以得取模一下            sum+=tmp*toll[idx];  //通话的分钟*该时间段的费用            tmp=1; //为了方便起见,像01:06:00这种整时的,算作下一小时的        }        else            tmp++;    }    if(tmp){        //比如说1:08:03,由于1:08:00的时候算作8-9之间的,实际上统计的tmp=4,所以要-1        sum+=(tmp-1)*toll[(ends%DAYMINUTE)/60];    }    return sum;}int main(){    char word[10];    for(int i=0;i<24;i++)        scanf("%d",&toll[i]);    scanf("%d",&n);    for(int i=0;i<n;i++){        scanf("%s %d:%d:%d:%d %s",record[i].name,&record[i].month,&record[i].day,&record[i].hour,&record[i].minute,word);        record[i].time=(record[i].day-1)*DAYMINUTE+record[i].hour*HOURMINUTE+record[i].minute;        if(word[1]=='n')            record[i].mark=ONLINE;        else            record[i].mark=OFFLINE;    }    sort(record,record+n);    int last=-1; //之前最近的一个on-line的索引    //找出配对的on-line和off-line,存入phone数组,方便后序处理    for(int i=0;i<n;i++){        if(record[i].mark==ONLINE){            last=i;        }        if(record[i].mark==OFFLINE && last!=-1){            if(strcmp(record[i].name,record[last].name)==0){                strcpy(phone[cnt].name,record[i].name);                phone[cnt].month=record[i].month;                phone[cnt].d1=record[last].day;                phone[cnt].h1=record[last].hour;                phone[cnt].m1=record[last].minute;                phone[cnt].time1=record[last].time;                phone[cnt].d2=record[i].day;                phone[cnt].h2=record[i].hour;                phone[cnt].m2=record[i].minute;                phone[cnt].time2=record[i].time;                cnt++;                last=-1;            }        }    }    int tot=0,sum=0;    for(int i=0;i<cnt;i++){        if(i==0){            printf("%s %02d\n",phone[i].name,phone[i].month);            sum=cal(i);            tot+=sum;            int len=phone[i].time2-phone[i].time1;            double cost=sum*1.0/100;            printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2lf\n",phone[i].d1,phone[i].h1,phone[i].m1,phone[i].d2,phone[i].h2,phone[i].m2,len,sum*1.0/100);        }        else{            if(strcmp(phone[i].name,phone[i-1].name)!=0){                printf("Total amount: $%.2lf\n",tot*1.0/100);                printf("%s %02d\n",phone[i].name,phone[i].month);                tot=0;            }            sum=cal(i);            tot+=sum;            int len=phone[i].time2-phone[i].time1;            int cost=sum*1.0/100;            printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2lf\n",phone[i].d1,phone[i].h1,phone[i].m1,phone[i].d2,phone[i].h2,phone[i].m2,len,sum*1.0/100);        }    }    if(tot){        printf("Total amount: $%.2lf\n",tot*1.0/100);    }    return 0;}

(4)这个没用容器竟然这么短

#include <cstdio>  #include <string>  #include <iostream>  #include <algorithm>    using namespace std;  int charge[30];    struct node{      string name, time, state;      bool operator<(const node & X) const{          if (name == X.name)              return time < X.time;          return name < X.name;      }  }Node[1010];    int get_time(const string & str) {      int total_time = 0;      total_time = (str[9] - '0') * 10 + (str[10] - '0');      total_time = ((str[6] - '0') * 10 + (str[7] - '0')) * 60 + total_time;      total_time = ((str[3] - '0') * 10 + str[4] - '0') * 24 * 60 + total_time;      return total_time;  }    int get_charge(int begin_time, int end_time) {      int total_charge = 0;      if (begin_time % 60 != 0) {          total_charge += charge[(begin_time / 60) % 24] * (60 + begin_time / 60 * 60 - begin_time);          begin_time = 60 + begin_time / 60 * 60;      }        for(; begin_time <= end_time; begin_time += 60)          total_charge += charge[(begin_time / 60) % 24] * 60;      if (begin_time > end_time)          total_charge -= charge[(end_time / 60) % 24] * 60 - charge[(end_time / 60) % 24] * (end_time - end_time / 60 * 60);      else          total_charge += charge[(end_time / 60) % 24] * (end_time - end_time / 60 * 60);        return total_charge;  }    int main() {      for (int i = 0; i < 24; ++i)          scanf("%d", charge + i);        int n;      scanf("%d", &n);        for (int i = 0; i < n; ++i) {          cin >> Node[i].name >> Node[i].time >> Node[i].state;      }        sort(Node, Node + n);      string month(Node[0].time.begin(), Node[0].time.begin() + 2);      string name;      int cur_charge = 0;        for (int i = 1; i < n; ++i) {          if (Node[i].state == "off-line" && Node[i - 1].state == "on-line" && Node[i].name == Node[i - 1].name) {              if (Node[i].name != name) {                  if (name != "")                      printf("Total amount: $%d.%02d\n", cur_charge / 100, cur_charge % 100);                  cur_charge = 0;                  name = Node[i].name;                  cout << name << " " << month << endl;              }                int total_time = get_time(Node[i].time) - get_time(Node[i - 1].time);              int total_charge = get_charge(get_time(Node[i - 1].time), get_time(Node[i].time));              cur_charge += total_charge;              cout << Node[i - 1].time.substr(3, 8) << " "                   << Node[i].time.substr(3, 8) << " "                   << total_time << " $";              printf("%d.%02d\n", total_charge / 100, total_charge % 100);          }      }      printf("Total amount: $%d.%02d\n", cur_charge / 100, cur_charge % 100);        return 0;  }  


原创粉丝点击