欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

1016 Phone Bills (25)

程序员文章站 2022-03-24 15:34:00
...
    这是鬼鬼珊的第一篇博客呀(作为学计算机的beauty girl,第一次写有一点不好意思
此处很想插入一个表情的,可是没有找到。。。。。)
    然后想要找到作为女生的自觉,设计一下这个页面布局,想想还是算了,因为就算有搜
到我这篇博客的非常非常非常稀少的人也是学计算机的理科生....嗯,不明觉厉.....
    之前从没想过在这上面写点什么,今天心血来潮,因为一道题做了一天,从早上十点开始,
到晚上九点才做对,真的很开心很开心,然后头脑发热,开始我的小作家旅程,哈哈
这个时候我其实突然想起了,我今天需要洗头啦。。。。终于体会到段公子说的那句话了,
学计算机的人,自带学计算机的气质,大概懂了,不过我写完博客之后还是会去洗头的,嘿嘿...

言归正传,进入正题。

1016 Phone Bills (25)(25 分)
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 10
10
CYLL 01:01:06:01 on-line
CYLL 01:28:16:05 off-line
CYJJ 01:01:07:00 off-line
CYLL 01:01:08:03 off-line
CYJJ 01:01:05:59 on-line
aaa 01:01:01:03 on-line
aaa 01:02:00:01 on-line
CYLL 01:28:15:41 on-line
aaa 01:05:02:24 on-line
aaa 01:04:23:59 off-line
Sample Output:

CYJJ 01
01:05:59 01:07:00 61 12.10Totalamount:12.10
CYLL 01
01:06:01 01:08:03 122 24.4028:15:4128:16:05243.85
Total amount: 28.25aaa0102:00:0104:23:594318638.80
Total amount: $638.80

注意点:
1、要按照名字的字母顺序进行排序,如果名字相同就需要根据时间。(这里我采用的将时间转换为分钟数,根据分钟数的大小来进行排列,而且还便于之后的总分钟数计算,比如1:10:10这个时间转换为分钟数即为(1-1)*1440+10*60+10=610,而2:03:05=(2-1)*1440+3*60+5=1625.根据分钟数大小即可知道后面这个时间一定在前面那个时间之后)
2、排序好之后,接下来是计算费用。最好要用cent[0-23]的下标来表示每个时间段的每分钟费用,这样可以根据时间的小时直接带入下标即可计算。
3、计算费用,首先我先用总的分钟数除以1440再乘以每天花费的money就可以得到整数天花费的费用。剩下的不到一天的时间:分为逻辑上的两类情况,第一种是开始时间和结束时间不在同一天,第二种是开始时间和结束时间在同一天的情况分别进行计算。在同一天的时候,要特别注意开始时间和结束时间在同一个小时的情况。
4、对于总的费用等于0的不合法输入,不能输出有关这个customer的任何信息,这一点一定要注意。
(以上仅是我这个白痴珊的注意点,大佬们可以忽略,代码也不是很简洁,不过我会努力的,嘿嘿)

#include <iostream>
#include<string>
#include<memory.h>
#include<stdlib.h>
#include<string.h>
#include<iomanip>
using namespace std;
typedef struct Node{
   char name[22];
   int time[4];
   string flag;
   long long minute;
}Record;
float OnedayMoney;
//存放每个时间段打电话每分钟的费用
int cent[24];
//存放电话记录个数
int n;
int month;//存放月份
string flagend="off-line";//存放结束标记
string flagstart="on-line";
//
void Inputcent(){
   int i;
   OnedayMoney=0;
   for(i=0;i<24;i++){
      cin>>cent[i];
      OnedayMoney=OnedayMoney+float(cent[i]*60)/100;
   }
}
long long datetominute(int time[4]){
    long long minute=0;
    minute=minute+(time[1]-1)*1440;
    minute=minute+(time[2])*60;
    minute=minute+time[3];
    return minute;
}
//输入每一条记录
void InputRecord(Record record[]){
  int i,j;
  char c;
  for(i=1;i<=n;i++){
      cin>>record[i].name;
      for(j=0;j<4;j++){
          cin>>record[i].time[j];
          cin.get(c);
      }
      cin>>record[i].flag;
      record[i].minute=datetominute(record[i].time);
  }
}
//交换
void swap(Record &record1,Record &record2)
{
    int i;
    Record temp;
    strcpy(temp.name,record1.name);
    for(i=1; i<4; i++)
    {
        temp.time[i]=record1.time[i];
    }
    temp.flag=record1.flag;
    temp.minute=record1.minute;
    strcpy(record1.name,record2.name);
    for(i=1; i<4; i++)
    {
        record1.time[i]=record2.time[i];
    }
    record1.flag=record2.flag;
    record1.minute=record2.minute;
    strcpy(record2.name,temp.name);
    for(i=1; i<4; i++)
    {
        record2.time[i]=temp.time[i];
    }
    record2.flag=temp.flag;
    record2.minute=temp.minute;
}
//根据姓名排序,将记录排列,如果姓名相同,就按时间进行排列
void Order(Record record[]){
    int i,j;
    for(i=1;i<n;i++){
        for(j=1;j<=n-i;j++){
            if(strcmp(record[j].name,record[j+1].name)>0||(strcmp(record[j].name,record[j+1].name)==0&&record[j].minute>record[j+1].minute)){
                swap(record[j],record[j+1]);
            }
        }
    }
}
//输出时间
void outputtime(int time[]){
   int i;
   for(i=1;i<3;i++){
    if(time[i]<10){
        cout<<0<<time[i]<<':';
    }
    else{
        cout<<time[i]<<':';
    }
   }
   if(time[3]<10){
    cout<<0<<time[3]<<' ';
   }
   else{
    cout<<time[3]<<' ';
   }

}
//计算费用
float Calculate(Record start,Record end){
    long long total;
    float Moneytemp=0;
    int i;
    total=end.minute-start.minute;
    cout<<total<<" $";
    Moneytemp=Moneytemp+total/1440*OnedayMoney;
    //分三种情况情况讨论
    if(total%1440==0){
        return Moneytemp;
    }
    else{
        //不在同一天结束且在一天之内(逻辑上)
        if(end.time[1]>start.time[1]&&(end.time[2]<start.time[2]||(end.time[2]==start.time[2]&&end.time[3]<start.time[3]))){
            Moneytemp=Moneytemp+float((cent[start.time[2]]*(60-start.time[3])))/100;
            for(i=start.time[2]+1;i<24;i++){
                Moneytemp=Moneytemp+float(cent[i]*60)/100;
            }
            for(i=0;i<end.time[2];i++){
                Moneytemp=Moneytemp+float(cent[i]*60)/100;
            }
            Moneytemp=Moneytemp+float(cent[end.time[2]]*end.time[3])/100;
        }
        //在同一天结束(逻辑上)
        else{
            //不在同一个小时
            if(end.time[2]>start.time[2]){
                Moneytemp=Moneytemp+float(cent[start.time[2]]*(60-start.time[3]))/100;
                for(i=start.time[2]+1;i<end.time[2];i++){
                 Moneytemp=Moneytemp+float(cent[i]*60)/100;
                }
                Moneytemp=Moneytemp+float(cent[end.time[2]]*end.time[3])/100;
            }
            //在一个小时
           else{
              Moneytemp=Moneytemp+float(cent[end.time[2]]*(end.time[3]-start.time[3]))/100;
           }

        }
    }
    cout<<setiosflags(ios::fixed)<<setprecision(2)<<Moneytemp<<endl;
    return Moneytemp;
}
//处理并输出函数
int Process(Record record[],int position)
{
    int i=position;
    int start,end;
    float MoneyEnd=0;
    bool outflag=true;
    while(strcmp(record[i].name,record[position].name)==0&&i<n)
    {

        if(record[i].flag==flagstart&&record[i+1].flag==flagend&&strcmp(record[i+1].name,record[position].name)==0)
        {
            if(outflag)
            {
                cout<<record[position].name<<' ';
                if(record[position].time[0]<10)
                {
                    cout<<0<<record[position].time[0]<<endl;
                }
                else
                {
                    cout<<record[position].time[0]<<endl;
                }
                outflag=false;
            }
            start=i;
            outputtime(record[start].time);
            i++;
            end=i;
            outputtime(record[end].time);
            MoneyEnd=MoneyEnd+Calculate(record[start],record[end]);
        }
        i++;
    }
    if(MoneyEnd>0){
         cout<<"Total amount: $"<<setiosflags(ios::fixed)<<setprecision(2)<<MoneyEnd<<endl;
    }
    return i;
}
int main()
{
   Inputcent();
   cin>>n;
   Record record[n+1];
   InputRecord(record);
   Order(record);
   int i=1;
   while(i<n){
    i=Process(record,i);
   }
    return 0;
}

看到这张图的时候,真的开心了很久很久
1016 Phone Bills (25)

相关标签: 算法设计