天天看点

1016 Phone Bills (25 分)(算时间差)

1016 Phone Bills (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.10
Total amount: $12.10
CYLL 01
01:06:01 01:08:03 122 $24.40
28:15:41 28:16:05 24 $3.85
Total amount: $28.25
aaa 01
02:00:01 04:23:59 4318 $638.80
Total amount: $638.80      

本题的难度在与题意的理解以及时间费用计算要掌握正确方法

卡了好久,出去跑了两圈回来,立刻就ac了。。。

难点1:题意理解

难点2:非法匹配不输出,存在有的没有正确匹配,连名字都不输出

难点3:计算时间差用当初看算法笔记时认为效率最低最low的写法,一天一天地加,是代码逻辑最简单的写法,本题用此几乎没了大难度。输出格式小心点即可

#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;

struct customer {
  string name, type, time;
}cust[1010];

int rate[25];//费率表

bool compare(customer a, customer b) {
  if (a.name != b.name) return a.name<b.name;
  return a.time<b.time;

}

int tempMin;
double tempRate;
//计算分钟差与费用  time1<time2 
void countM(string time1, string time2) {
  tempMin = 0, tempRate = 0;
  int t1[4], t2[4];
  sscanf(time1.c_str(), "%d:%d:%d:%d", &t1[0], &t1[1], &t1[2], &t1[3]);//只计算t[1]~t[3]  1天-2时-3分
  sscanf(time2.c_str(), "%d:%d:%d:%d", &t2[0], &t2[1], &t2[2], &t2[3]);//只计算t[1]~t[3]  天-时-分
  //费率
  /* 天        时     分
  t2[1]    t2[2]   t2[3]
  t1[1]    t1[2]   t1[3]
  */
  while(t1[1]<t2[1]||t1[2]<t2[2]||t1[3]<t2[3]){
    t1[3]++;//一直加 总有一天会加到相等  太妙了 很妙的写法 
    tempMin++;
    tempRate+=rate[t1[2]];
    if(t1[3]==60){
      t1[3]=0;
      t1[2]++;
    }
    
    if(t1[2]==24){
      t1[2]=0;
      t1[1]++;
    }
  }
  
}


int main() {

  //freopen("in.txt", "r", stdin);

  for (int i = 0;i<24;i++)  cin >> rate[i];
  
  int N;
  cin >> N;
  for (int i = 0;i<N;i++) {
    cin >> cust[i].name >> cust[i].time >> cust[i].type;
  }
  sort(cust, cust + N, compare);
  string nowName = "";
  int MM, dd, hh, min;
  double total = 0.0;
  bool outName=false; 
  for (int i = 0;i<N - 1;i++) {
    sscanf(cust[i].time.c_str(), "%d:%d:%d:%d", &MM, &dd, &hh, &min);
    //换人 
    if (cust[i].name != nowName) {
      if (i != 0&&total!=0){
        printf("Total amount: $%.2f\n", total / 100.0);//没有的不能输出0  ★★ 这个bug难调 唉! 
      }
      total = 0;
      outName=false;
      nowName = cust[i].name;
       
    }

    //判断匹配  i匹配i+1 
    if (cust[i + 1].name != nowName) continue;
    if (cust[i].type == "off-line") continue;
    if (cust[i + 1].type == "on-line") continue;
    /*判断完毕 下一个记录还是nowName且当前on-line 下一个off-line*/
    //输出
    countM(cust[i].time, cust[i + 1].time);
    //现在发现 还是结构体复杂一点好  多练 这种题目还有好多  不怕  越做越会 
    if(!outName){
      printf("%s %02d\n", nowName.c_str(), MM);//非法的连名字都不能输出  烦人
      outName=true;//此人输出过名字了 
    }
    cout << cust[i].time.c_str() + 3 << " " << cust[i + 1].time.c_str() + 3 << " " << tempMin;
    printf(" $%.2f\n", tempRate / 100.0);

    //计费
    total += tempRate;

  }
  
  //也要判断才能输出  万一正好最后一组无匹配成功呢?
  if(total!=0) printf("Total amount: $%.2f", total/100.00);
  return 0;
}      
#include<bits/stdc++.h>
using namespace std;
struct customer {
  string name, type, time;
}cust[1010];
bool compare(customer a, customer b) {
  if (a.name != b.name) return a.name<b.name;
  return a.time<b.time;

}
int rate[25];//费率表
int tempMin,tempRate;
void countM(string time1, string time2) {//计算分钟差与费用  time1<time2 
  tempMin = 0, tempRate = 0;
  int t1[4], t2[4];
  sscanf(time1.c_str(), "%d:%d:%d:%d", &t1[0], &t1[1], &t1[2], &t1[3]);//只计算t[1]~t[3]  1天-2时-3分
  sscanf(time2.c_str(), "%d:%d:%d:%d", &t2[0], &t2[1], &t2[2], &t2[3]);//只计算t[1]~t[3]  天-时-分
  while(t1[1]<t2[1]||t1[2]<t2[2]||t1[3]<t2[3]){
    t1[3]++;//一直加 总有一天会加到相等  太妙了 很妙的写法 
    tempMin++;
    tempRate+=rate[t1[2]];
    if(t1[3]==60){
      t1[3]=0;
      t1[2]++;
    }
    if(t1[2]==24){
      t1[2]=0;
      t1[1]++;
    }
  }
}

int main() {
  for (int i = 0;i<24;i++)  cin >> rate[i];
  int N;
  cin >> N;
  for (int i = 0;i<N;i++) {
    cin >> cust[i].name >> cust[i].time >> cust[i].type;
  }
  sort(cust, cust + N, compare);
  string nowName = "";
  int MM, dd, hh, min;
  double total = 0.0;
  bool outName=false; 
  for (int i = 0;i<N - 1;i++) {
    sscanf(cust[i].time.c_str(), "%d:%d:%d:%d", &MM, &dd, &hh, &min);
    if (cust[i].name != nowName) {
      if (i != 0&&total!=0) printf("Total amount: $%.2f\n", total / 100.0);//没有的不能输出0  ★★ 这个bug难调 唉! 
      total = 0;outName=false;
      nowName = cust[i].name;
    }
    if (cust[i + 1].name != nowName||cust[i].type == "off-line"||cust[i + 1].type == "on-line") continue;
    countM(cust[i].time, cust[i + 1].time);
    if(!outName){
      printf("%s %02d\n", nowName.c_str(), MM);//非法的连名字都不能输出  烦人
      outName=true;//此人输出过名字了 
    }
    cout << cust[i].time.c_str() + 3 << " " << cust[i + 1].time.c_str() + 3 << " " << tempMin;
    printf(" $%.2f\n", tempRate / 100.0);
    total += tempRate;
  }
  if(total!=0) printf("Total amount: $%.2f", total/100.00);
  return 0;
}