1016 Phone Bills

2022/2/2 23:12:36

本文主要是介绍1016 Phone Bills,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1016 Phone Bills

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

我的代码,测试点1可以通过,后面的都不能通过,问题应该是未能处理当某用户没有合法记录时不进行任何输出

//问题应该是当某用户未能正确配对时不进行输出 

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

struct Record
{
  char id[25];
  int month;
  int day;
  int hour;
  int minute;
  int flag;
}Re[1000];

bool cmp(Record r1, Record r2)
{
  if (strcmp(r1.id, r2.id))
  {
    return strcmp(r1.id, r2.id) < 0;
  }
  else if (r1.month != r2.month)
  {
    return r1.month < r2.month;
  }
  else if (r1.day != r2.day)
  {
    return r1.day < r2.day;
  }
  else if(r1.hour != r2.hour)
  {
    return r1.hour < r2.hour;
  }
  else
  {
    return r1.minute < r2.minute;
  }
}

int main()
{
  int rate[24];
  for (int i = 0; i < 24; i++)
  {
    scanf_s("%d", &rate[i]);
  }  

  int N=0;
  scanf_s("%d", &N);
  char line[10];
  for (int i = 0; i < N; i++)
  {
    scanf_s("%s %d:%d:%d:%d %s", &Re[i].id, sizeof(char) * 15, &Re[i].month, &Re[i].day, &Re[i].hour, &Re[i].minute, &line, sizeof(char) * 10);
    if (strcmp(line,"on-line")==0)
    {
      Re[i].flag = 1;  //on-line
    }
    else 
    {
      Re[i].flag = 2;  //off-line
    }
  }

  sort(Re, Re + N, cmp);
  int people = 0;  //记录用户是否已经开始输出
  int diff = 0;  //记录时长
  float price = 0;  //单笔费用
  float total = 0;  //总费用
  for (int i = 0; i < N; i++)
  {
    if (i > 0)
    {
      if (strcmp(Re[i].id, Re[i-1].id)!=0)
      {
        people = 0;
      }
      else
      {
        people = 1;
      }
    }
    if (people == 0)
    {
      if (i > 0)
      {
        printf("Total amount: $%.02f\n", total/100 );
      }
      printf("%s %02d\n", Re[i].id, Re[i].month);
      total = 0;
    }

    if (Re[i].flag == 1)
    {
      if (Re[i + 1].flag == 2)
      {
        printf("%02d:%02d:%02d ", Re[i].day, Re[i].hour, Re[i].minute);
      }
      else
        continue;
    }
    if (Re[i].flag == 2)
    {
      if (Re[i - 1].flag == 1)
      {
        printf("%02d:%02d:%02d ", Re[i].day, Re[i].hour, Re[i].minute);
      }
      else
        continue;
    }


    price = 0;
    if (Re[i].flag == 2)  //off-line
    {
      if (Re[i].day == Re[i - 1].day)
      {
        if (Re[i].hour == Re[i - 1].hour)
        {
          diff = Re[i].minute - Re[i - 1].minute;
          price = diff * rate[Re[i].hour];
        }
        else
        {
          diff = (Re[i].hour - Re[i - 1].hour) * 60 + Re[i].minute - Re[i - 1].minute;
          for (int j = Re[i - 1].hour + 1; j <= Re[i].hour - 1; j++)
          {
            price += 60 * rate[j];
          }
          price = price + (60 - Re[i - 1].minute) * rate[Re[i - 1].hour] + Re[i].minute * rate[Re[i].hour];
        }
      }
      else
      {
        diff=(Re[i].day-Re[i-1].day)*1440+ (Re[i].hour - Re[i - 1].hour) * 60 + Re[i].minute - Re[i - 1].minute;
        for (int k = Re[i - 1].day + 1; k <= Re[i].day - 1; k++)
        {
          price += 21300;
        }
        for (int j = Re[i - 1].hour + 1; j <= 23; j++)
        {
          price += 60 * rate[j];
        }
        for (int j = 0; j <= Re[i].hour - 1; j++)
        {
          price += 60 * rate[j];
        }
        price = price + (60 - Re[i - 1].minute) * rate[Re[i - 1].hour] + Re[i].minute * rate[Re[i].hour];
      }
      

      total += price;
      printf("%d ", diff);
      printf("$%.02f\n", price/100);

    }

    
  }
  printf("Total amount: $%.02f\n", total / 100);
}


参考代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int toll[25];

struct Record
{
  char id[25];
  int month;
  int day;
  int hour;
  int minute;
  int flag;
}rec[1000],temp;

bool cmp(Record r1, Record r2)
{
  if (strcmp(r1.id, r2.id))
  {
    return strcmp(r1.id, r2.id) < 0;
  }
  else if (r1.month != r2.month)
  {
    return r1.month < r2.month;
  }
  else if (r1.day != r2.day)
  {
    return r1.day < r2.day;
  }
  else if (r1.hour != r2.hour)
  {
    return r1.hour < r2.hour;
  }
  else
  {
    return r1.minute < r2.minute;
  }
}

void get_ans(int on, int off, int& time, int& money)
{
  //得到某条通话记录的花费
  temp = rec[on];
  while (temp.day < rec[off].day || temp.hour < rec[off].hour || temp.minute < rec[off].minute)
  {
    time++;
    money += toll[temp.hour];
    temp.minute++;
    if (temp.minute >= 60)
    {
      temp.minute = 0;
      temp.hour++;
    }
    if (temp.hour >= 24)
    {
      temp.hour = 0;
      temp.day++;
    }
  }
}

int main()
{
  for (int i = 0; i < 24; i++)
  {
    scanf_s("%d", &toll[i]);
  }
  int n;
  scanf_s("%d", &n);
  char line[10];
  for (int i = 0; i < n; i++)
  {
    scanf_s("%s", rec[i].id, sizeof(char) * 25);
    scanf_s("%d:%d:%d:%d", &rec[i].month, &rec[i].day, &rec[i].hour, &rec[i].minute);
    scanf_s("%s", &line, sizeof(char) * 10);
    if (strcmp(line, "on-line") == 0)
    {
      rec[i].flag = 1;
    }
    else
    {
      rec[i].flag = 0;
    }
  }
  sort(rec, rec + n, cmp);

  int on = 0, off, next;  //on和off为配对的两条记录,next为下一个用户
  while (on < n)
  {
    //每次循环处理单个用户的所有记录
    int needPrint = 0;  //表示该用户是否需要输出
    next = on;
    while (next < n && strcmp(rec[next].id, rec[on].id) == 0)
    {
      if (needPrint == 0 && rec[next].flag == 1)
      {
        needPrint = 1;  //找到on
      }
      else if (needPrint == 1 && rec[next].flag == 0)
      {
        needPrint = 2;  //在on之后找到off
      }
      next++;
    }
    if (needPrint < 2)
    {
      //没有找到配对的on-off
      on = next;
      continue;
    }
    int allmoney = 0;  //该用户总共的花费
    printf("%s %02d\n", rec[on].id, rec[on].month);
    while (on < next)
    {
      //寻找该用户所有的配对
      while (on < next - 1 && !(rec[on].flag == 1 && rec[on + 1].flag == 0))
      {
        //直到找到连续的on和off
        on++;
      }
      off = on + 1;
      if (off == next)
      {
        //已经输出完毕所有配对的on和off
        on = next;
        break;
      }
      printf("%02d:%02d:%02d ", rec[on].day, rec[on].hour, rec[on].minute);
      printf("%02d:%02d:%02d ", rec[off].day, rec[off].hour, rec[off].minute);
      int time = 0, money = 0;
      get_ans(on, off, time, money);
      allmoney+=money;
      printf("%d $%.2f\n", time, money / 100.0);
      on = off + 1;  //完成一个配对,从off+1开始找下一对
    }
    printf("Total amount: $%.2f\n", allmoney / 100.0);
  }
}


这篇关于1016 Phone Bills的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程