首页 > 试题广场 >

Phone Bills (25)

[编程题]Phone Bills (25)
  • 热度指数:4082 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 640M,其他语言1280M
  • 算法知识视频讲解
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.

输入描述:
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.


输出描述:
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.
示例1

输入

10 10 10 10 10 10 20 20 20 15 15 15 15 15 15 15 20 30 20 15 15 10 10 10<br/>10<br/>CYLL 01:01:06:01 on-line<br/>CYLL 01:28:16:05 off-line<br/>CYJJ 01:01:07:00 off-line<br/>CYLL 01:01:08:03 off-line<br/>CYJJ 01:01:05:59 on-line<br/>aaa 01:01:01:03 on-line<br/>aaa 01:02:00:01 on-line<br/>CYLL 01:28:15:41 on-line<br/>aaa 01:05:02:24 on-line<br/>aaa 01:04:23:59 off-line

输出

CYJJ 01<br/>01:05:59 01:07:00 61 $12.10<br/>Total amount: $12.10<br/>CYLL 01<br/>01:06:01 01:08:03 122 $24.40<br/>28:15:41 28:16:05 24 $3.85<br/>Total amount: $28.25<br/>aaa 01<br/>02:00:01 04:23:59 4318 $638.80<br/>Total amount: $638.80
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int charge[24];  //资费
struct input{
    char name[25]; //姓名
    int mon,day,hour,min; //月,日,时,分
    bool line;  //on还是off
}ip[1010],temp,id;
bool cmp(input a,input b){
    if(strcmp(a.name,b.name)!=0) return strcmp(a.name,b.name)<0; //按字典序将姓名排序
    else if(a.mon!=b.mon) return a.mon<b.mon;
    else if(a.day!=b.day) return a.day<b.day;
    else if(a.hour!=b.hour) return a.hour<b.hour;
    else return a.min<b.min;
}
void getans(int on,int off,int &time,int &cost){
    temp=ip[on];
    while(temp.day<ip[off].day || temp.hour<ip[off].hour || temp.min<ip[off].min){
        time++;  //总分钟增加
        cost+=charge[temp.hour];  //话费增加
        temp.min++;  //当前时间增加1min
        if(temp.min>=60){
            temp.min=0;
            temp.hour++;   //下一个小时
        }
        if(temp.hour>=24){
            temp.hour=0;
            temp.day++;   //下一天
        }
    }
}
int main(){
    for(int i=0;i<24;i++) scanf("%d",&charge[i]);  //输入每个小时不同的资费
    int x;scanf("%d",&x);  //记录数
    char str[25];
    for(int i=0;i<x;i++){
        scanf("%s %d:%d:%d:%d %s",ip[i].name,&ip[i].mon,&ip[i].day,&ip[i].hour,&ip[i].min,str); //输入记录
        if(strcmp(str,"on-line")==0) ip[i].line=true;
        else ip[i].line=false;
    }
    sort(ip,ip+x,cmp);  //排序
    id=ip[0];int p,num=0,sum=0;   //id用来暂时保存同一个用户的姓名,p为记录on还是off,num记录匹配对数,sum记录同一用户的总资费
    for(int i=0;i<x;i++){
        if(strcmp(id.name,ip[i].name)==0){   //若是相同一个用户
            if(ip[i].line==true) p=1;        //on-line则p为1
            else{
                if(p==1){                    //当前记录为off-line且上一条是on-line
                    int time=0,cost=0;num++;
                    if(num==1) printf("%s %02d\n",ip[i].name,ip[i].mon); //在输出第一对匹配信息前先输出用户姓名与月份
                    getans(i-1,i,time,cost);  //求出本次通话时间与资费
                    sum+=cost;
                    printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n",ip[i-1].day,ip[i-1].hour,ip[i-1].min,ip[i].day,ip[i].hour,ip[i].min,time,cost/100.0); 
                }
                p=0; //off-line则p为0
            }
        }
        else{             //下一个用户
            id=ip[i];
            num=0;sum=0;
            if(ip[i].line==true) p=1;
            else p=0;
        }
        if(strcmp(id.name,ip[i+1].name)!=0){  //若下一条记录是另一个用户,则把当前用户的总资费输出(如果有合法匹配的通话记录的话)
            if(num>0)
                printf("Total amount: $%.2f\n",sum/100.0);  //直接/100.0 就不用先设double型变量
        }
        
    }
    return 0;
}

发表于 2018-01-21 19:59:30 回复(0)
//大致思路:
//就是把每一个用户的所有时间按大小排序(online 和offline一起排),因为是按照时间陪对的;
//选一个时间 t1 再看接下来的时间 t2 是不是offline(同时选的时间要是online),否则忽略 t1,继续往后面选
(写的有点繁琐,大致思路明白就行)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <math.h>
#include <vector>
#include <map>
#include <queue>

using namespace std;
const int maxn = 1001;
struct Customer {
    string name;
    map<string, string> record;  //当前用户的时间线
    vector<int> lasting_time;  //每个时间对 所对应的持续时间
    vector<int> cost; //每个时间对 对应的花费
    int total_cost;
    string month;
};

struct Record {
    string time;
    string status;
};
vector<Record> time_line[maxn];
map<string, int> mp;  //用于映射用户在用于数组中的位置,因为map中string自动排好序了,方便输出
Customer customers[maxn];
int price[24];

bool cmp(Record a, Record b) {
    return a.time<b.time;
}

int ctoi(char c) {
    return (int)c -48;
}

int getMinutes(string time) {
    int day = ctoi(time[3])*10 + ctoi(time[4]);
    int hour = ctoi(time[6])*10 + ctoi(time[7]);
    int minute = ctoi(time[9])*10 + ctoi(time[10]);

    return day*24*60 + hour*60 + minute;
}
//获取时间段内的花费,(这里写的有点麻烦...)
int getPrice(string start_time, string end_time) {
    int start_day = ctoi(start_time[3])*10 + ctoi(start_time[4]);
    int start_hour = ctoi(start_time[6])*10 + ctoi(start_time[7]);
    int start_minute = ctoi(start_time[9])*10 + ctoi(start_time[10]);

    int end_day = ctoi(end_time[3])*10 + ctoi(end_time[4]);
    int end_hour = ctoi(end_time[6])*10 + ctoi(end_time[7]);
    int end_minute = ctoi(end_time[9])*10 + ctoi(end_time[10]);

    int cost = 0;
    if(start_day<end_day) {
        cost += price[start_hour] * (60-start_minute);
        for(int j=start_hour+1; j<=23; j++) {
            int toll = price[j];
            cost += toll*60;
        }

        if(end_day>start_day+1) {
            int whole_day_cost = 0;
            for(int j=0; j<=23; j++) {
                whole_day_cost += price[j]*60;
            }
            cost += whole_day_cost* (end_day-start_day-1);
        }

        for(int j=0; j<end_hour; j++) {
            int toll = price[j];
            cost += toll*60;
        }
        cost += price[end_hour] * end_minute;
    } else {
        if(start_hour<end_hour) {
            cost += price[start_hour] * (60-start_minute);
            cost += price[end_hour] * end_minute;
            for(int j=start_hour+1; j<end_hour; j++) {
                int toll = price[j];
                cost += toll*60;
            }
        } else {
            cost += price[start_hour] * (end_minute-start_minute);
        }
    }

    return cost;
}

int main() {
    for(int i=0; i<24; i++) {
        scanf("%d",price+i);
    }

    int n;
    scanf("%d",&n);
    int cnt = 0;
    for(int i=0; i<n; i++) {
        string name,time,status;
        cin>>name>>time>>status;

        int index;
        if(mp.find(name) != mp.end()) {  //判断当前用户是否已经添加过
            index = mp[name];
        } else {
            mp[name] = cnt;
            index = cnt;
            Customer temp;
            temp.name = name;
            temp.total_cost = 0;
            customers[cnt] = temp;
            cnt++;
        }

        Record r;
        r.time = time;
        r.status = status;
        time_line[index].push_back(r);

    }

    for(int i=0; i<cnt; i++) {
        sort(time_line[i].begin(), time_line[i].end(), cmp);
    }

    for(int i=0; i<cnt; i++) {
        int len = time_line[i].size();
        for(int j=0; j<len-1; ) {
            Record r = time_line[i][j];
            Record next_r = time_line[i][j+1];
            if(r.status == "on-line" && next_r.status == "off-line") {
                string start_time_str = r.time;
                string end_time_str = next_r.time;
                //customers[i].record[start_time] = end_time;
                int start_mm = getMinutes(start_time_str);
                int end_mm = getMinutes(end_time_str);
                int last_time = end_mm - start_mm;

                if(last_time>=0) {
                    string month = start_time_str.substr(0,2);
                    int cost = getPrice(start_time_str, end_time_str);
                    customers[i].record[start_time_str.substr(3,8)] = end_time_str.substr(3,8);
                    customers[i].lasting_time.push_back(last_time);
                    customers[i].cost.push_back(cost);
                    customers[i].total_cost += cost;
                    customers[i].month = month;
                }
                j += 2;
            } else {
                j++;
            }
        }
    }

    for(map<string, int>::iterator it = mp.begin(); it!=mp.end(); it++) {
        int c_index = it->second;
        Customer customer = customers[c_index];
        if(customer.total_cost>0) {
            cout<<customer.name<<" "<<customer.month<<endl;
            int i=0;
            for(map<string, string>::iterator it2 = customer.record.begin(); it2!=customer.record.end(); it2++,i++) {
                cout<<it2->first<<" "<<it2->second<<" "<<customer.lasting_time[i]<<" $";
                printf("%0.2f\n",(float)customer.cost[i]/100);
            }
            printf("Total amount: $%0.2f\n",(float)customer.total_cost/100);
        }

    }
}


发表于 2019-08-25 19:07:25 回复(0)
思路:首先把问题的大意说一下吧。
就是按照名字的顺序打印电话单。
(不用考虑跨月打,还是挺好的。)
on-line 可以理解为接起电话
off-line 理解为挂断电话
你首先把数据按照时间排序,然后如果一个online数据下面一个数据不是offline就删掉。因为是垃圾
数据,然后你把剩下的数据计算时间输出。我也不知道为啥写了这么多如果考试3个小时写一道
还差不多。
#include <iostream>
#include <fstream>
#include <string>
#include <deque>
#include <map>
#include <algorithm>
#include <iomanip>
#include <cstdio>
using namespace std;

#ifdef debug
ifstream ifile("case1.txt");
#define cin ifile
#endif

int hourRate[24] = { 0 };
double cents = 0; // 一日总消耗的钱财

struct data
{
    string name;
    int time[4] = { -1 };// 月 日 时 分
    bool onOff = false;
    data()
    {
        time[0] = -1;//默认初始化
    }
};
struct data sNULL; //

bool Cmp(struct data & a, struct data & b)
{
    // 首先按照时间排序
    if (a.time[0] != b.time[0])
    {
        return a.time[0] < b.time[0];
    }
    else if (a.time[1] != b.time[1])
    {
        return a.time[1] < b.time[1];
    }
    else if (a.time[2] != b.time[2])
    {
        return a.time[2] < b.time[2];
    }
    else
        return a.time[3] < b.time[3];
}

int CalTimeLast(struct data & a, struct data & b)
{
    // b > a; 假定在一个月内
    int sumA = 0;
    int sumB = 0;
    sumA = a.time[1] * 60 * 24 + a.time[2] * 60 + a.time[3];
    sumB = b.time[1] * 60 * 24 + b.time[2] * 60 + b.time[3];
    return sumB - sumA;
}

double CalMoney(struct data a, struct data b, int sum)// 反回 cent
{
    // 假定在一个月内
    double money = 0;
    //时间算前不算后  前面的零碎的时间 中间 60分整时间 后面的零碎的时间
    if (b.time[1] - a.time[1] >= 2)
    {
        money += cents * (b.time[1] - a.time[1]-1);
        b.time[1] = a.time[1] + 1;
    }
    if (60 - a.time[3] < sum)
    {
        sum -= 60 - a.time[3];
        money += (60 - a.time[3]) * hourRate[a.time[2]];
        a.time[2]++;
    }
    else
    {
        money += sum * hourRate[a.time[2]];
        return money;
    }
    // 已经取整了 小时遍历
    if (a.time[1] == b.time[1])
    {
        for (int startHour = a.time[2]; startHour != b.time[2]; startHour++)
        {
            money += hourRate[startHour] * 60;
            sum -= 60;
        }
    }
    else
    {
        for (int startHour = a.time[2]; startHour < 24 + b.time[2]; startHour++)
        {
            money += hourRate[startHour % 24] * 60;
            sum -= 60;
        }
    }
    money += hourRate[b.time[2]] * b.time[3];
    return money;    
}

class customer
{
public:
    string name;
    deque<struct data> datas;

public:
    struct data CleanData()
    {
        sort(this->datas.begin(), this->datas.end(), Cmp);// 把自己的记录已经排号序了
        for (auto it = this->datas.begin(); it != this->datas.end();)
        {
            if (it + 1 != this->datas.end())
            {
                if (it->onOff == true && (it + 1)->onOff == false)
                {
                    it+=2;
                }
                else
                {
                    it = this->datas.erase(it);
                }
            }
            else
            {
                this->datas.erase(it);
                break;
            }
        }// 无效数据 已经被清洗
        if (this->datas.size())
        {
            return this->datas[0];
        }
        return sNULL;
    }
    double Print()
    {
        double sumDoller = 0;
        cout << this->name << " ";
        cout << setfill('0') << setw(2) << this->datas[0].time[0] << endl;
        for (int i = 0; i < this->datas.size(); i++,i++)
        {
            cout << setfill('0') << setw(2) << this->datas[i].time[1] << ":";
            cout << setfill('0') << setw(2) << this->datas[i].time[2] << ":";
            cout << setfill('0') << setw(2) << this->datas[i].time[3] << " ";
            cout << setfill('0') << setw(2) << this->datas[i + 1].time[1] << ":";
            cout << setfill('0') << setw(2) << this->datas[i + 1].time[2] << ":";
            cout << setfill('0') << setw(2) << this->datas[i + 1].time[3] << " ";
            cout << CalTimeLast(this->datas[i], this->datas[i + 1]) << " $";
            double tmp = (CalMoney(this->datas[i], this->datas[i + 1], CalTimeLast(this->datas[i], this->datas[i + 1])) / 100.0);
            sumDoller += tmp;
            printf("%.2f\n", tmp);
        }
        printf("Total amount: $%.2f\n", sumDoller);
        return sumDoller;
    }
};
bool Cmp1(customer & a, customer & b)
{
    // 首先按照姓名排序
    return a.name < b.name;
}


int main()
{
    for (int i = 0; i < 24; i++)
    {
        cin >> hourRate[i];
    }
    int N;
    cin >> N;
    struct data v;
    string tmp;
    map<string, int> mp;
    deque<customer> vC;
    
    int index = 1;
    for (int i = 0; i < N; i++)
    {
        cin >> v.name;
        getline(cin, tmp);
        sscanf(tmp.c_str(),"%d:%d:%d:%d", &v.time[0], &v.time[1], &v.time[2], &v.time[3]);
        if (tmp[tmp.size() - 6] == 'n')
            v.onOff = true;
        else
            v.onOff = false;
        if (mp[v.name] == 0)
        {
            // 新建 类对象
            customer p;
            p.name = v.name;
            p.datas.push_back(v);
            vC.push_back(p);
            mp[v.name] = index++;
        }
        else
        {
            vC[mp[v.name]-1].datas.push_back(v);
        }
    }// 数据输入完毕

    // 数据都归于类 排名输出序列
    for (int i=0; i < vC.size(); )
    {
        if (vC[i].CleanData().time[0] == -1)
        {
            vC.erase(vC.begin() + i);
        }
        else
        {
            i++;
        }
    }
    for (int i = 0; i < 24; i++)
        cents += hourRate[i] * 60;
    sort(vC.begin(), vC.end(), Cmp1);
    for (int i = 0; i < vC.size(); i++)
    {
        vC[i].Print();
    }
    system("pause");
    return 0;
}
编辑于 2018-08-31 14:51:53 回复(0)
大佬解答一下,我的哪里错了,给的测试过了

#include<stdio.h>

#include<iostream>

#include<string>

#include<string.h>

#include<vector>

#include<algorithm>

using namespace std;


struct record

{

    char name[21];

    char status[11];

    int month,day,hour,minute;

    bool isOnline;

};


int cmp(record a,record b)

{

    int name = strcmp(a.name,b.name);

    if(name!=0)

        return name<0 ;

    if(a.day-b.day!=0)

        return a.day<b.day;

    if(a.hour-b.hour!=0)

        return a.hour<b.hour;

    

    return a.minute<b.minute;


};

int main()

{

    char online[11]="on-line";

    int hourRate[24];

    for(int i=0;i<=23;i++)

    {

        scanf("%d",&hourRate[i]);

    }

    int N;

    scanf("%d",&N);

    vector<record> records;

    for(int i=0;i<N;i++)

    {

        record tmp;

        scanf("%s %d:%d:%d:%d %s",tmp.name,&tmp.month,&tmp.day,&tmp.hour,&tmp.minute,tmp.status);

        if(strcmp(tmp.status,online)==0)

        {

            tmp.isOnline = true;

        }else 

        {

            tmp.isOnline = false;

        }

        records.push_back(tmp);

    }

    //排序

    sort(records.begin(),records.end(),cmp);

    //开始计算账单

    

    bool startFlag = false;

    char currentName[21]="";

    double currentMoney = 0;

    bool currentOutput = false;

    for(int i=0;i<records.size();i++)

    {

        

        

        if(records[i].isOnline)

        {

            startFlag = true;

            currentOutput= false;

            continue;

        }

        

        if(startFlag)

        {

            currentOutput = false;

            startFlag = false;

            record start = records[i-1];

            record end = records[i];

            if(strcmp(currentName,"")==0)

            {

                //output name

                strcpy(currentName,start.name);

                currentMoney = 0;

                printf("%s %02d\n",start.name,start.month);

            }else if(strcmp(currentName,start.name)!=0)

            {

                printf("Total amount: $%0.2lf\n",currentMoney);

                //output name

                strcpy(currentName,start.name);

                currentMoney = 0;

                printf("%s %02d\n",start.name,start.month);

                currentOutput = true;

            }

            

            //计算钱了

            int tmpday=start.day,tmphour=start.hour,tmpminute=start.minute;

            int totalMinute = 0;

            double money = 0;

    

            while(1)

            {

                

                money+=hourRate[tmphour];

                tmpminute++;

                totalMinute++;

                if(tmpminute>59)

                {

                    tmpminute=0;

                    tmphour++;

                    if(tmphour>23)

                    {

                        tmphour=0;

                        tmpday++;

                    }

                }

                if(tmpday==end.day&&tmphour==end.hour&&tmpminute==end.minute)

                {

                    //达到末尾时间

                    money /=100;

                    currentMoney+=money;

                    printf("%02d:%02d:%02d %02d:%02d:%02d %d $%0.2lf\n",start.day,start.hour,start.minute,end.day,end.hour,end.minute,totalMinute,money);

                    //cout<<totalMinute<<" - "<<money/2<<endl;

                    

                                    

                    break;

                }

            }

        }

    }

    if(!currentOutput)

    {

        printf("Total amount: $%0.2lf\n",currentMoney);

    }


    return 0;

}


发表于 2018-02-18 21:20:59 回复(0)
用Java写的,通过率80%
先贴上代码,后续再修改
package go.jacob.day920;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

/**
 * 1016. Phone Bills (25)
 * 
 * @author Jacob 1.首先建立相应的数据结构
 *
 */
public class Demo1 {
    static int[] hourCost;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // 记录每个时间段的花费
        hourCost = new int[24];
        for (int i = 0; i < 24; i++) {
            hourCost[i] = sc.nextInt();
        }
        int n = sc.nextInt();
        // 数字输出格式
        DecimalFormat df = new DecimalFormat("00");
        DecimalFormat df1 = new DecimalFormat(".00");
        // 读取每条记录,并存储在相应数据结构中

        ArrayList<Call> allCalls = new ArrayList<Call>();
        for (int i = 0; i < n; i++) {
            Call tmp = new Call();
            tmp.name = sc.next();
            String time = sc.next();
            String[] times = time.split(":");
            tmp.month = Integer.parseInt(times[0]);
            tmp.date = Integer.parseInt(times[1]);
            tmp.hour = Integer.parseInt(times[2]);
            tmp.minute = Integer.parseInt(times[3]);
            tmp.status = sc.next();
            allCalls.add(tmp);
        }
        // 对所有的元素进行排序
        MyComparator compare = new MyComparator();
        Collections.sort(allCalls, compare);

        // 删除无效记录
        ArrayList<Call> formatCalls = new ArrayList<Call>();

        for (int i = 0; i < allCalls.size(); i++) {
            Call curCall = allCalls.get(i);
            // 判断call能否真正放入format
            solve(formatCalls, curCall);
            // 还要判断最后一个是否为on-line
            if (i == allCalls.size() - 1) {
                Call tmp = formatCalls.get(formatCalls.size() - 1);
                if (tmp.status.equals("on-line"))
                    formatCalls.remove(formatCalls.size() - 1);
            }
        }
        // 现在format中的Call元素都是合法配对的数据
        double sum = 0;
        for (int i = 0; i < formatCalls.size(); i += 2) {
            if (i == 0) {
                System.out.println(formatCalls.get(i).name + " " + df.format(formatCalls.get(i).month));
            }
            // 如果i不为0,且与上一call不属于同一个人
            if (i != 0 && !formatCalls.get(i).name.equals(formatCalls.get(i - 1).name)) {
                System.out.println("Total amount: $" + df1.format(sum / 100));
                System.out.println(formatCalls.get(i).name + " " + df.format(formatCalls.get(i).month));
                sum = 0;
            }
            Call cur = formatCalls.get(i), next = formatCalls.get(i + 1);
            double[] res = calTime(cur, next);
            sum += res[1];
            System.out.println(df.format(cur.date) + ":" + df.format(cur.hour) + ":" + df.format(cur.minute) + " "
                    + df.format(next.date) + ":" + df.format(next.hour) + ":" + df.format(next.minute) + " "
                    + df.format(res[0]) + " $" + df1.format(res[1] / 100));

        }
        System.out.println("Total amount: $" + df1.format(sum / 100));
        sc.close();
    }

    private static double[] calTime(Call cur, Call next) {
        // res[0]为所花费时间 res[1]为所花费的钱
        double[] res = new double[2];
        res[0] = next.getTotal() - cur.getTotal();
        res[1] = getMoney(next.getTotal()) - getMoney(cur.getTotal());

        return res;
    }

    // 计算从月初经历total分钟的花费
    private static int getMoney(int total) {
        int hours = total / 60;
        int minutes = total % 60;
        int sum = 0;
        for (int i = 0; i < hours; i++) {
            sum += hourCost[i % 24] * 60;
        }
        sum += hourCost[hours % 24] * minutes;
        return sum;
    }

    /*
     * 根据preCall和curCall来生成foamat集合
     */
    private static void solve(ArrayList<Call> formatCalls, Call curCall) {
        if (formatCalls.isEmpty()) {
            // 如果集合为空,只有当当前为on-line时才能放入集合
            if (curCall.status.equals("on-line")) {
                formatCalls.add(curCall);
            }
            return;
        }

        Call preCall = formatCalls.get(formatCalls.size() - 1);
        // 如果是同一个人的通话记录
        if (preCall.name.equals(curCall.name)) {
            if (preCall.status.equals("on-line")) {
                // 如果两个都为on-line,保留最新的一个
                if (curCall.status.equals("on-line")) {
                    formatCalls.remove(formatCalls.size() - 1);
                    formatCalls.add(curCall);
                } else {// 如果当前通话记录为off-line,加入集合
                    formatCalls.add(curCall);
                }
            } else {// preCall的status为off-line,只有当前通话记录为on-line时才加入集合
                if (curCall.status.equals("on-line"))
                    formatCalls.add(curCall);
            }
        } else {// 不是同一个人的通话记录
            // 如果上一个人的最后一个通话记录是on-line,非法记录,删除
            if (preCall.status.equals("on-line"))
                formatCalls.remove(formatCalls.size() - 1);
            // 如果当前通话记录(新的一个人)为on-line,加入集合
            if (curCall.status.equals("on-line"))
                formatCalls.add(curCall);

        }

    }
}

class MyComparator implements Comparator<Call> {

    // 按名字,时间排序
    @Override
    public int compare(Call c1, Call c2) {
        // String判断不能用==
        if (!c1.name.equals(c2.name))
            return c1.name.compareTo(c2.name);
        else if (c1.month != c2.month)
            return c1.month - c2.month;
        else if (c1.date != c2.date)
            return c1.date - c2.date;
        else if (c1.hour != c2.hour)
            return c1.hour - c2.hour;
        else if (c1.minute != c2.minute)
            return c1.minute - c2.minute;
        else
            return 0;
    }

}

/*
 * 电话记录数据结构
 */
class Call {
    public String name;
    int month;
    int date;
    int hour;
    int minute;
    int total;
    String status;

    // 计算相对于月初的时间
    public int getTotal() {
        total = date * 24 * 60 + hour * 60 + minute;
        return total;
    }

    // 生成toString方法便于调试
    @Override
    public String toString() {
        return "Call [name=" + name + ", month=" + month + ", date=" + date + ", hour=" + hour + ", minute=" + minute
                + ", status=" + status + "]";
    }

}

编辑于 2017-09-20 13:34:00 回复(1)
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
using namespace std;
#define maxn 1010
struct record{
	char name[25];
	int mo,dd,hh,mm;
	bool flag;
}all[maxn],valid[maxn];
int toll[24],n;
double total;
map<string,int>Count;
bool cmp(record a,record b){
	if(strcmp(a.name,b.name)!=0)return strcmp(a.name,b.name)<0;
	else if(a.mo!=b.mo)return a.mo<b.mo;
	else if(a.dd!=b.dd)return a.dd<b.dd;
	else if(a.hh!=b.hh)return a.hh<b.hh;
	else return a.mm<b.mm;
}
void compute(record a,record b){
	int time=0,dt,ht,mt;
	double money=0;
	dt=a.dd,ht=a.hh,mt=a.mm;
	while(dt<b.dd||ht<b.hh||mt<b.mm){
		time++;
		mt++;
		money+=toll[ht]/100.0;
		if(mt==60)mt=0,ht++;
		if(ht==24)ht=0,dt++;
	}
	printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n",a.dd,a.hh,a.mm,b.dd,b.hh,b.mm,time,money);
	total+=money;
}
int main(){
	//freopen("A1016.txt","r",stdin);
	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",all[i].name,&all[i].mo,&all[i].dd,&all[i].hh,&all[i].mm);
		char temp[10];
		scanf("%s",temp);
		if(strcmp(temp,"on-line")==0)all[i].flag=true;
		else all[i].flag=false;
	}
	sort(all,all+n,cmp);
	int num=0;
	for(int i=0;i<n-1;i++){
		if(strcmp(all[i].name,all[i+1].name)==0&&all[i].flag&&!all[i+1].flag){
			valid[num++]=all[i];
			valid[num++]=all[i+1];
			if(Count.find(all[i].name)==Count.end())Count[all[i].name]=1;
			else Count[all[i].name]++;
		}
	}
	for(int i=0;i<num;){
		printf("%s %02d\n",valid[i].name,valid[i].mo);
		total=0;
		int temp=Count[valid[i].name];
		for(int k=0;k<temp;k++){
			compute(valid[i],valid[i+1]);
			i=i+2;
		}
		printf("Total amount: $%.2f\n",total);
	}
//	for(int i=0;i<num;i++)printf("%s %02d:%02d:%02d:%02d\n",valid[i].name,valid[i].mo,
//	valid[i].dd,valid[i].hh,valid[i].mm);
	return 0;
}
1.排序
2.剔除无效记录
3.用map<string,int>记录特定用户的通话次数,方便后期输出
发表于 2017-08-21 23:04:08 回复(0)
献丑了,自己的代码实在太长,不过提交后告诉我排在第三位,一个惊喜...
初学者,思路毕竟平凡,认为本题目的关键就在于【对无效记录的清除】以及【通话时间和话费在跨天等诸多情况下的计算】,再注意一些输出,胆大心细即可通过。
欢迎大家不吝批评指正,感激不尽。祝大家学习进步。
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iomanip>
using namespace std;

int Toll[24];
struct Record{
int d;
int h;
int m;
bool on;
string time;
Record(string tim,string da)
{
time = tim;
d = time[1]-'0'+(time[0]-'0')*10;
h = time[4]-'0'+(time[3]-'0')*10;
m = time[7]-'0'+(time[6]-'0')*10;
if(da[1]=='n') 
on = 1;
else 
on = 0;
};
bool const operator <(Record r) const
{
return time<r.time;
};
};
struct Usr{
string name;
int month;
vector<Record> rec;
Usr(string n,string mon): name(n)
{
month = (mon[0]-'0')*10+mon[1]-'0';
}
bool const operator>(const Usr u) const
{
return name<u.name;
};
};
double Cal(Record a, Record b, int f);
int main()
{
int num;
for(int i=0; i<24; i++) cin>>Toll[i];
cin>>num;
vector<Usr> Que;

for(int i=0; i<num; i++)
{
string name,time,on;
cin>>name>>time>>on;
int j=0;
for(j = 0; j<Que.size(); j++)
if(Que[j].name==name)
break;
if(j==Que.size()) //需要新插入
{
Usr a(name,time);
Que.push_back(a);
}
time = time.substr(3);
Que[j].rec.push_back(Record(time,on));
}

for(int i=0; i<Que.size(); i++)
{
sort(Que[i].rec.begin(),Que[i].rec.end(),less<Record>());
//如何快速消除无效记录
vector<Record>::iterator it = Que[i].rec.begin();
bool flag = 1;
for(int j=0; j<Que[i].rec.size()-1; j++)
{
if(!(Que[i].rec[j].on&&!Que[i].rec[j+1].on))
{
it = Que[i].rec.erase(it);
j--;
}
else
{
j++;
it+=2;
continue;
}
}
if(Que[i].rec.size()%2!=0)
Que[i].rec.pop_back();
}
sort(Que.begin(),Que.end(),greater<Usr>());

for(int i=0 ;i<Que.size(); i++)
{
double total = 0;
for(int j=0; j<Que[i].rec.size(); j+=2)
{
total += Cal(Que[i].rec[j],Que[i].rec[j+1],0);
}
if(total==0) continue;
cout<<Que[i].name<<" ";
if(Que[i].month>=10)
cout<<Que[i].month<<endl;
else
cout<<"0"<<Que[i].month<<endl;
for(int j=0; j<Que[i].rec.size(); j+=2)
{
cout<<Que[i].rec[j].time<<" "<<Que[i].rec[j+1].time<<" ";
total += Cal(Que[i].rec[j],Que[i].rec[j+1],1);
}
cout<<"Total amount: $"<<fixed<<setprecision(2)<<total/2<<endl;
}
return 0;
}

double Cal(Record a, Record b, int flag)
{
//输出分钟数和钱数
double money = 0;
int min = 0;
int d1 = a.d, d2 = b.d,
h1 = a.h, h2 = b.h,
m1 = a.m, m2 = b.m;
while(d1<=d2)
{
if(d1<d2)
{
while(h1<24)
{
min += 60-m1;
money += Toll[h1]*(60-m1);
m1 = 0;
h1++;
}
h1 %= 24;
}
else//在同一天
{
while(h1<=h2)
{
if(h1<h2)
{
min += 60 -m1;
money += Toll[h1]*(60-m1);
m1 = 0;
}
else //h1==h2 
{
min += m2-m1;
money += Toll[h1]*(m2-m1);
m1 = m2;
}
h1++;
}
}
d1++;
}
money /=100;
if(flag)
cout<<min<<" $"<<fixed<<setprecision(2)<<money<<endl;
return money;
}

发表于 2016-08-06 18:50:49 回复(0)
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>

#define N 24
using namespace std;

vector<int> timecost(N);
vector<float> cost(24 * 60 + 1);
struct Record
{
    string name{};
    string time{};
    string flag{};
};

struct Bill
{
    string name{};
    int month{};
    int date1{};
    int date2{};
    string onlinetime{};
    string offlinetime{};
    int suspendminutes{ 0 };
    float cost{ 0 };
};

int getmin(Record r)
{
    return (r.time[9] - '0') * 10 + (r.time[10] - '0');
}
int gethour(Record r)
{
    return (r.time[6] - '0') * 10 + (r.time[7] - '0');
}
int getdate(Record r)
{
    return (r.time[3] - '0') * 10 + (r.time[4] - '0');
}
bool cmp(const Record& r1, const Record& r2)
{
    if (r1.name != r2.name)
        return r1.name < r2.name;
    else
    {
        if (getdate(r1) != getdate(r2))
            return getdate(r1) < getdate(r2);
        else
        {
            return gethour(r1) * 60 + getmin(r1) < gethour(r2) * 60 + getmin(r2);
        }
    }
}

float getmoney(Record r1, Record r2)
{
    float totalmoney{ 0 };
    totalmoney = cost[gethour(r2) * 60 + getmin(r2)] - cost[gethour(r1) * 60 + getmin(r1)];
    return totalmoney;
}

int main()
{
    for (int i = 0; i < N; ++i)
    {
        cin >> timecost[i];
    }
    cost[0] = 0;
    for (int k = 1; k < 24 * 60 + 1; ++k)
    {
        if (k % 60 != 0)
            cost[k] = cost[k - 1] + timecost[k / 60] * 0.01;
        else
            cost[k] = cost[k - 1] + timecost[k / 60 - 1] * 0.01;
    }
    int n;
    cin >> n;
    vector<Record> v_r;
    map<string, vector<Bill>> customer;
    while (n--)
    {
        Record record;
        cin >> record.name >> record.time >> record.flag;
        v_r.push_back(record);
    }
    sort(v_r.begin(), v_r.end(), cmp);
    for (int j = 0; j < v_r.size() - 1; ++j)
    {
        if (v_r[j].name == v_r[j + 1].name && v_r[j].flag == "on-line" && v_r[j + 1].flag == "off-line")
        {
            Bill bill;
            bill.name = v_r[j].name;
            bill.month = (v_r[j].time[0] - '0') * 10 + (v_r[j].time[1] - '0');
            bill.date1 = getdate(v_r[j]);
            bill.date2 = getdate(v_r[j + 1]);
            bill.onlinetime = v_r[j].time.substr(3);
            bill.offlinetime = v_r[j + 1].time.substr(3);
            if (bill.date1 == bill.date2)
            {
                bill.suspendminutes = gethour(v_r[j + 1]) * 60 + getmin(v_r[j + 1]) - (gethour(v_r[j]) * 60 + getmin(v_r[j]));
                bill.cost = getmoney(v_r[j], v_r[j + 1]);
            }
            else
            {
                bill.suspendminutes = (bill.date2 - bill.date1 - 1) * 24 * 60 + (24 * 60 - gethour(v_r[j]) * 60 - getmin(v_r[j])) + (gethour(v_r[j + 1]) * 60 + getmin(v_r[j + 1]));
                bill.cost = (bill.date2 - bill.date1 - 1) * cost[24 * 60];
                bill.cost += cost[24 * 60] - cost[gethour(v_r[j]) * 60 + getmin(v_r[j])];
                bill.cost += cost[gethour(v_r[j + 1]) * 60 + getmin(v_r[j + 1])];
            }
            j++;
            if (customer.count(bill.name))
            {
                customer[bill.name].push_back(bill);
            }
            else
            {
                vector<Bill> billvector;
                billvector.push_back(bill);
                customer[bill.name] = billvector;
            }
        }
    }
    if (customer.size() == 0)
        return 0;
    for (auto kv : customer)
    {
        cout << kv.first << " ";
        printf("%02d\n", kv.second[0].month);
        float totalcost{ 0 };
        for (int i = 0; i < kv.second.size(); ++i)
        {
            Bill temp = kv.second[i];
            cout << temp.onlinetime << " " << temp.offlinetime << " " << temp.suspendminutes << " ";
            printf("$%.2f\n", temp.cost);
            totalcost += temp.cost;
        }
        printf("Total amount: $%.2f\n", totalcost);

    }
    return 0;
}
发表于 2023-03-03 09:57:27 回复(0)
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <stack>


using namespace std;

struct cus
{
    string name;
    int month;
    int day;
    int h;
    int m;
    string mode;
}c[1001];


// 
bool cmp_name(cus a, cus b)
{
    return a.name < b.name;
}


bool cmp_line(vector<string> a, vector<string> b)
{
    return a[0] < b[0];
}

// 将数字转换成字符串,如果只有一位则补0
string numTostr(int temp)
{
    if (temp >= 10)
        return to_string(temp);
    else
    {
        string a = to_string(temp);
        a = "0" + a;
        return a;
    }
}


int main()
{
    // 读取价格
    int cent[24];
    for (int i = 0; i < 24; i++)
        cin >> cent[i];
    
    int num;
    cin >> num;

    // 读取信息到结构体
    for (int i = 0; i < num; i++)
    {
        cin >> c[i].name;
        scanf("%d:%d:%d:%d", &c[i].month, &c[i].day, &c[i].h, &c[i].m);
        cin >> c[i].mode;
    }

    // 根据名字排序,大的放后面,即升序
    sort(c, c+num, cmp_name);

    for (int i = 0; i != num;)
    {
        // 查找相同姓名的内容,一同存储
        string name = c[i].name;
        int month = c[i].month;

        // 将on-line和off-line存储在同一个容器中
        vector<string> line[num];
        int index = 0;

        int j = i;
        for (; j < num; j++)
        {
            if (name == c[j].name)
            {
                // 获取 day h 和 m 转换成字符串用于拼接
                string day = numTostr(c[j].day);
                string h = numTostr(c[j].h);
                string m = numTostr(c[j].m);
                string time = day + h + m;
                // 将 on-line 和 off-line 分别存储
                if (c[j].mode == "on-line")
                {
                    line[index].push_back(time);
                    line[index].push_back("on-line");
                    index++;
                }  
                else
                {
                    line[index].push_back(time);
                    line[index].push_back("off-line");
                    index++;
                }   
            }
            else
                break;
        }
        i = j;

        // 升序排序
        sort(line, line+index, cmp_line);

        vector<string> finial_on, finial_off;
        vector<double> finial_money;
        vector<int> finial_min;

        // 将off_line和on_line对应起来
        for (int k = 0; k < index - 1; k++)
        {
            if (line[k][1] == "on-line" && line[k+1][1] == "off-line")
            {
                string on = line[k][0];
                string off = line[k+1][0];
                k++;
                double cal_money = 0;
                // 得到int类型的时间数据
                int day_on = stoi(on.substr(0, 2));
                int h_on = stoi(on.substr(2, 2));
                int m_on = stoi(on.substr(4, 2));

                int day_off = stoi(off.substr(0, 2));
                int h_off = stoi(off.substr(2, 2));
                int m_off = stoi(off.substr(4, 2));

                // 总分钟
                int total_min = 0;
                // 同一天
                if (day_on == day_off)
                {
                    // 如果跨小时的话
                    if (h_on < h_off)
                    {
                        // 计算分钟
                        total_min += 60 - m_on;
                        // 先计算剩余的时间
                        cal_money += (60 - m_on) * cent[h_on];
                        for (int time = h_on + 1; time < h_off; time++)
                        {
                            cal_money += 60*cent[time];
                            total_min += 60;
                        }
                        cal_money += m_off*cent[h_off];
                        total_min += m_off;

                    }
                    else if (h_on == h_off)
                    {
                        // 同一小时的话
                        cal_money += (m_off - m_on) * cent[h_on];
                        total_min += m_off - m_on;
                    }
                }
                else
                {
                    // 跨天
                    // 首先计算打电话当天用了多少钱
                    cal_money += (60 - m_on)*cent[h_on];
                    total_min += (60 - m_on);
                    for (int time = h_on + 1; time < 24; time++)
                    {
                        cal_money += 60 * cent[time];
                        total_min += 60;
                    }
                        
                    // 再计算打电话到挂电话的天数
                    for (int day_now = day_on + 1; day_now < day_off; day_now++)
                    {
                        for (int q = 0; q < 24; q++)
                        {
                            cal_money += 60*cent[q];
                            total_min += 60;
                        }
                    }

                    // 最后计算挂电话当天花了多少钱
                    for (int time = 0; time < h_off; time++)
                    {
                        cal_money += 60*cent[time];
                        total_min += 60;
                    }
                        
                    cal_money += m_off*cent[h_off];
                    total_min += m_off;
                }

                // 将得到的金钱和时间段存储起来
                finial_on.push_back(on);
                finial_off.push_back(off);
                finial_money.push_back(cal_money);
                finial_min.push_back(total_min);
            }
        }

        double total = 0;
        for (int p = 0; p < finial_money.size(); p++)
            total += finial_money[p];
        
        if (total != 0)
        {
            cout << name << " " << numTostr(month) << endl;

            for (int p = 0; p < finial_on.size(); p++)
            {
                cout << finial_on[p].substr(0, 2) << ":" << finial_on[p].substr(2, 2) << ":" << finial_on[p].substr(4, 2);
                cout << " ";
                cout << finial_off[p].substr(0, 2) << ":" << finial_off[p].substr(2, 2) << ":" << finial_off[p].substr(4, 2);
                cout << " ";
                cout << finial_min[p] << " ";
                printf("$%.2lf\n", finial_money[p]/100);
            }
            printf("Total amount: $%.2lf\n", total/100);
        }
    
    }
    return 0;
}

发表于 2022-09-10 22:32:28 回复(0)
#include<map>
#include<cstdio>
#include<string>
#include<vector>
#include <iostream>
#include<algorithm>
using namespace std;
double price_min[24];
int N;
struct record {
	int dd, hh, mm, t;
	string tag;
	record(int dd1, int hh1, int mm1, int t1, string tag1)
	{
		dd = dd1;
		hh = hh1;
		mm = mm1;
		t = t1;
		tag = tag1;
	}
};
map<string, vector<record>>m;
bool cmp(record a, record b)
{
	return a.t < b.t;
}
int main()
{
	int i,month;
	for (i = 0; i < 24; i++)
	{
		double temp;
		cin >> temp;
		price_min[i] = temp;
	}
	cin >> N;
	for (i = 0; i < N; i++)
	{
		int dd, hh, mm, t;
		string tag,name;
		char c;
		cin >> name >> month >> c >> dd >> c >> hh >> c >> mm >> tag;
		t = 1440 * dd + 60 * hh + mm;
		m[name].emplace_back(dd, hh, mm, t, tag);
	}
	for (auto it = m.begin(); it != m.end(); it++)
	{
		auto v = it->second;
		double total = 0;
		sort(v.begin(), v.end(),cmp);
		for (i = 0; i < v.size()-1;)
		{
			double fenzhang = 0;
			if (v[i].tag > v[i + 1].tag)
			{
				if (total == 0)
				{
					cout << it->first;
					printf(" %02d\n", month);
				}
				int start_time = v[i].t;
				int end_time = v[i + 1].t;
				int total_time = 0;
				for (; start_time < end_time; start_time++,total_time++)
					fenzhang += price_min[start_time % 1440 / 60];
				printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2lf\n", v[i].dd, v[i].hh, v[i].mm, v[i + 1].dd, v[i + 1].hh, v[i + 1].mm, total_time, fenzhang / 100);
				total += fenzhang;
			}
			if (fenzhang == 0)
				i++;
			else
				i += 2;
		}
		if (total > 0)
			printf("Total amount: $%.2lf\n", total / 100);
	}
	return 0;

}



发表于 2021-02-01 10:56:44 回复(0)
来一波 短小精悍!!!
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
struct node{
	string state;
	int MM,dd,hh,mm,toally;
	node(string state,int MM,int dd,int hh,int mm,int toally):state(state),MM(MM),dd(dd),hh(hh),mm(mm),toally(toally){}
};
bool cmp(node a,node b){return a.toally<b.toally;}
double charge[25],dayspend=0;
double caulate(node a){//直接计算从第一天开始就在通话的费用
	double money=(a.dd-1)*dayspend;
	for(int i=0;i<a.hh;i++)
		money+=charge[i]*60;
	money+=charge[a.hh]*a.mm;
	return money;
}
int rate,n,MM,dd,hh,mm;
set<string> ans;//存入有效账户
string name,state;
map<string,vector<node>> Hash;
int main(){
	for(int i=0;i<24;i++){
		scanf("%d", &rate);
		charge[i]=(rate*1.0)/100;
		dayspend+=charge[i]*60;
	}
	scanf("%d", &n);
	for(int i=0;i<n;i++){
		cin>>name;
		scanf("%d:%d:%d:%d",&MM,&dd,&hh,&mm);
		cin>>state;
		ans.insert(name);
		Hash[name].push_back(node(state,MM,dd,hh,mm,dd*24*60+hh*60+mm));
	}
	for(auto it=ans.begin();it!=ans.end();it++){
		sort(Hash[*it].begin(),Hash[*it].end(),cmp);
		int flag=0,tot;
		double toally=0;
		for(int i=0;i<Hash[*it].size()-1;i++){
			if(Hash[*it][i].state=="on-line" && Hash[*it][i+1].state =="off-line"){
				double temp=caulate(Hash[*it][i+1])-caulate(Hash[*it][i]);
				toally+=temp;
				tot=Hash[*it][i+1].toally-Hash[*it][i].toally;
				if(!flag){
				cout<<*it;
				printf(" %02d\n",Hash[*it][i].MM);
				flag=1;
				}
				printf("%02d:%02d:%02d",Hash[*it][i].dd,Hash[*it][i].hh,Hash[*it][i].mm);
				printf(" %02d:%02d:%02d %d $%.2f\n",Hash[*it][i+1].dd,Hash[*it][i+1].hh,Hash[*it][i+1].mm,tot,temp);
				i++;
			}
		}
		if(flag)  printf("Total amount: $%.2f\n",toally);
	}
	return 0;
}

发表于 2021-01-27 22:52:06 回复(0)
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
/**
 * 根据名字和时间先排序,在做筛选不合理的剔除
 * @return
 */
class record{
public:
    string name;
    int mon,day,hour,min;
    string status;
};
bool cmp(record a,record b){
    if(a.name!=b.name) return a.name<b.name;
    if(a.day!=b.day) return a.day<b.day;
    if(a.hour!=b.hour) return a.hour<b.hour;
    return a.min<b.min;
}

int main() {
    int price[24];int sum=0;
    for (int i = 0; i < 24; ++i) {
        cin>>price[i];sum+=price[i];
    }
    sum=sum*60;
    int N;cin>>N;
    vector<record> v;
    for (int i = 0; i < N; ++i) {
        record rec;cin>>rec.name;scanf("%d:%d:%d:%d",&rec.mon,&rec.day,&rec.hour,&rec.min);cin>>rec.status;
        v.push_back(rec);
    }
    sort(v.begin(),v.end(),cmp);
    string name="";//上一个人的名字
    double  allBills=0;
    for (int i = 0; i < v.size();) {
        record a=v[i],b;
        if(i+1<v.size()){b=v[i+1];}
        if(a.name!=b.name) {
            i++;
            continue;
        }
        //取两个名字一样的人记录
        if(name!=a.name){//换人了,打印总账单
            // 上一个人的账单
            if(allBills!=0)
                printf("Total amount: $%.2lf\n",allBills);
            allBills=0;
        }
        if(a.status=="on-line"&&b.status=="off-line"){//两个状态互补
            int temp=(b.day-a.day)*24*60+(b.hour-a.hour)*60+(b.min-a.min);
            double bill=0;
            if(b.day==a.day){
                if(a.hour==b.hour){
                    bill+=(b.min-a.min)*price[a.hour];
                }else{
                    for (int j =a.hour; j <=b.hour ; ++j) {
                        if(j==a.hour) {
                            bill+=(60-a.min)*price[j];
                        }else if(j==b.hour){
                            bill+=b.min*price[j];
                        }else{
                            bill+=60*price[j];
                        }
                    }
                }
            }else{
                bill+=(b.day-a.day-1)*sum;
                for (int j =a.hour; j <24 ; ++j) {
                    if(j==a.hour) {
                        bill+=(60-a.min)*price[j];
                    }else{
                        bill+=60*price[j];
                    }
                }
                for (int j =0; j <=b.hour ; ++j) {
                    if(j==b.hour){
                        bill+=b.min*price[j];
                    }else{
                        bill+=60*price[j];
                    }
                }
            }
            bill/=100.0;
            if(name!=a.name){
                name=a.name;
                cout<<a.name<<" ";printf("%02d\n",a.mon);//打印新人的开头
            }
            printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2lf\n",a.day,a.hour,a.min,b.day,b.hour,b.min,temp,bill);
            allBills+=bill;i+=2;
        }else{
            i++;continue;
        }
    }
    if(allBills!=0)  printf("Total amount: $%.2lf\n",allBills);
    return 0;
}


发表于 2021-01-23 19:13:15 回复(0)
/*
完美通过全部样例
要注意账单为0就不输出这个人了
*/
#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<bits/stdc++.h>

#define INF 2147483647
#define MIN INF+1
#define ll long long

using namespace std;

// 记录结构体 
struct rec {
	int M, d, H, m;
	string s;
};

// 用于记录排序,chronologically  
bool cmp(rec r1, rec r2) {
	
	bool order1 = true;
	bool order2 = false;
	
	if(r1.M < r2.M) {
		return order1;
	} else if(r1.M > r2.M) {
		return order2;
	} else {
		
		if(r1.d < r2.d) {
			return order1;
		} else if(r1.d > r2.d) {
			return order2;
		} else {
			
			if(r1.H < r2.H) {
				return order1;
			} else if(r1.H > r2.H) {
				return order2;
			} else {
				
				if(r1.m < r2.m) {
					return order1;
				} else if(r1.m > r2.m) {
					return order2;
				} else {
					return order2;
				}
				
			}
			
		}
		
	}
	
}

// 用户结构体 
struct cus {
	string name = "";
	vector<rec> v;
	int bill = 0;
	int month;
};

int main() {    
	// std::ios::sync_with_stdio(false);
    // std::cin.tie(0);
    
	double rate[24];
	int N;
	map<string, cus*> tree;
	
	for(int i = 0; i < 24; i++) {
		cin >> rate[i];
		rate[i] /= 100.0;
	} 
	cin >> N;
	cus c[N];
	for(int i = 0; i < N ; i++) {
		string name;
		int MM, dd, HH, mm;
		string status;
		cin >> name;
		scanf("%d:%d:%d:%d", &MM, &dd, &HH, &mm);
		cin >> status;
		if(tree.find(name) == tree.end()) {
			tree[name] = new cus;
			tree[name] -> name = name;
			tree[name] -> month = MM;
		}
		rec r;
		r.M = MM;
		r.d = dd;
		r.H = HH;
		r.m = mm;
		r.s = status;
		vector<rec> new_rec = tree[name] -> v;
		new_rec.push_back(r);
		tree[name] -> v = new_rec; 
	}
	
	// 模拟题哪里比较烦呢 就是编程一时爽 找bug火葬场 
	for(auto x:tree) {
		
		
		/****************测试账单是否为0(直接复制的后面的代码)其实没啥用,就是不加这个过不去********************/ 
		// 用大括号扩一下作用域(笑 
		{
			vector<rec> bills = x.second->v;
			// 按照时间顺序排序 
			sort(bills.begin(), bills.end(), cmp);
			double total_sum_bill = 0;
			rec s;
			s.s = "NULL";
			for(auto i:bills) {
				if(i.s == "on-line") {
					s = i;
				}
				if(i.s == "off-line") {
					if(s.s != "NULL") {
						rec op = s;
						rec np = i;
						s.s = "NULL";
						// printf("%02d:%02d:%02d ", op.d, op.H, op.m);
						// printf("%02d:%02d:%02d ", np.d, np.H, np.m);
						
						int dur = 0;
						dur += (np.d - op.d) * 24 * 60;
						dur += (np.H - op.H) * 60;
						dur += np.m - op.m;
						// cout << dur << " $";
						double this_sum_bill = 0;
						for(int i = op.d*24*60 + op.H*60 + op.m; i < np.d*24*60 + np.H*60 + np.m; i++) {
							this_sum_bill += rate[(i/60)%24];
						}
						// printf("%.2f\n", this_sum_bill);
						total_sum_bill += this_sum_bill;
					}
				}
			}
			if(total_sum_bill == 0) {
				continue;
			}
		}
		/***************************/ 
		
		
		
		cout << x.first << ' ';
		printf("%02d\n", x.second->month);
		vector<rec> bills = x.second->v;
		
		// 按照时间顺序排序 
		sort(bills.begin(), bills.end(), cmp);

		rec s;
		s.s = "NULL";
		double total_sum_bill = 0;
		for(auto i:bills) {
			if(i.s == "on-line") {
				s = i;
			}
			if(i.s == "off-line") {
				if(s.s != "NULL") {
					rec op = s;
					rec np = i;
					s.s = "NULL";
					printf("%02d:%02d:%02d ", op.d, op.H, op.m);
					printf("%02d:%02d:%02d ", np.d, np.H, np.m);
					
					int dur = 0;
					dur += (np.d - op.d) * 24 * 60;
					dur += (np.H - op.H) * 60;
					dur += np.m - op.m;
					cout << dur << " $";
					double this_sum_bill = 0;
					for(int i = op.d*24*60 + op.H*60 + op.m; i < np.d*24*60 + np.H*60 + np.m; i++) {
						this_sum_bill += rate[(i/60)%24];
					}
					printf("%.2f\n", this_sum_bill);
					total_sum_bill += this_sum_bill;
				}
			}
		}
		cout << "Total amount: $";
		printf("%.2f\n", total_sum_bill); 
	}
	
	return 0;
} 

发表于 2020-09-17 18:46:54 回复(0)
就剩用例0没有通过,哪位大佬能帮忙看看。
#include<algorithm>
(831)#include<string>
#include<cstdio>
(802)#include<cmath>
using namespace std;
struct stu{
	string id;
	int m;
	int d;
	int h;
	int mi;
	string state;
	int f;		//f=1为有效数据,f=0为无效数据
};
bool cmpa(stu a,stu b){				//根据名字与时间排序
	if(a.id!=b.id){
		return a.id<b.id;
	}else if(a.m!=b.m){
		return a.m<b.m;
	}else if(a.d!=b.d){
		return a.d<b.d;
	}else if(a.h!=b.h){
		return a.h<b.h;
	}else if(a.mi!=b.mi){
		return a.mi<b.mi;
	}
}
int main(){
	stu s[1500],r[1500];
	char id[25],state[10];
	int N,f=0,m[24],n=0,d,h,mi;		//n为总分钟
	double sum=0,fsum=0;		//sum为每轮总金额,fsum为每个人总金额
	//输入数据
	for(int i=0;i<24;i++){
		scanf("%d",&m[i]);
	}
	scanf("%d",&N);
	for(int i=0;i<N;i++){
		scanf("%s %d:%d:%d:%d %s",id,&s[i].m,&s[i].d,&s[i].h,&s[i].mi,state);
		s[i].id=id;
		s[i].state=state;
		s[i].f=0;
	}
	//按照姓名与时间排序
	sort(s,s+N,cmpa);
	//去除无效通话记录,r为排序好的有效数据
	for(int i=0;i<N-1;i++){
		if(s[i].id==s[i+1].id&&s[i].state=="on-line"&&s[i+1].state=="off-line"){
			s[i].f=1;
			s[i+1].f=1;
		}
	}
	for(int i=0;i<N-1;i++){
		if(s[i].f==1)r[f++]=s[i];
	}
	//每两个数据一组,统计时间和钱,每个人一个总计
	printf("%s %02d\n",r[0].id.c_str(),r[0].m);	
	d=r[0].d;h=r[0].h;mi=r[0].mi;
	while(r[0].d<r[1].d||r[0].h<r[1].h||r[0].mi<r[1].mi){
		r[0].mi++;
		n++;
		sum+=m[r[0].h];
		if(r[0].mi==60){
			r[0].mi=0;
			r[0].h++;
		}
		if(r[0].h==24){
			r[0].h=0;
			r[0].d++;
		}
	}
	sum=sum/100;
	fsum+=sum;
	printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n",d,h,mi,r[1].d,r[1].h,r[1].mi,n,sum);
	sum=0;
	n=0;
	for(int i=2;i<f-1;i+=2){				//每组+2的加
		if(r[i].id!=r[i-1].id){
			printf("Total amount: $%.2f\n",fsum);
			fsum=0;
			printf("%s %02d\n",r[i].id.c_str(),r[i].m);	
		}
		d=r[i].d;h=r[i].h;mi=r[i].mi;
		while(r[i].d<r[i+1].d||r[i].h<r[i+1].h||r[i].mi<r[i+1].mi){
			r[i].mi++;
			n++;
			sum+=m[r[i].h];
			if(r[i].mi==60){
				r[i].mi=0;
				r[i].h++;
			}
			if(r[i].h==24){
				r[i].h=0;
				r[i].d++;
			}
		}
		sum=sum/100;
		fsum+=sum;
		printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n",d,h,mi,r[i].d,r[i].h,r[i].mi,n,sum);
		sum=0;
		n=0;
	}
	printf("Total amount: $%.2f\n",fsum);

	return 0;
}


发表于 2020-03-02 00:19:38 回复(0)
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct call{
	char name[30];
	int d,h,m,of;
}cal[1010];
int cmp(call a,call b){
	if(strcmp(a.name,b.name)) return strcmp(a.name,b.name)<0;
	if(a.d!=b.d) return a.d<b.d;
	if(a.h!=b.h) return a.h<b.h;
	return a.m<b.m;
}
int per[30];
void getsum(call a,call b,int &fen,double &sum){
	fen=0;
	sum=0;
	int d=a.d,h=a.h,m=a.m;
	while(!(d==b.d&&h==b.h&&m==b.m)){
		fen++;
		sum=sum+per[h]*0.01;
		m++;
		if(m==60){
			m=0;h++;
			if(h==24){
				h=0;
				d++;
			}
		}
	}
}
int main(){
//	int per[24];
	for(int i=0;i<24;++i) scanf("%d",per+i);
	int n,month;
	char ison[15];
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%s %d:%d:%d:%d %s",cal[i].name,&month,&cal[i].d,&cal[i].h,&cal[i].m,ison);
		if(strcmp(ison,"on-line")==0) cal[i].of=0;
		else cal[i].of=1;
	}
	sort(cal,cal+n,cmp);
	int i=0,next,is;
	while(i<n){
		is=0;
		next=i;
		while(next<n&&strcmp(cal[next].name,cal[i].name)==0){
			if(cal[next].of==0) is=-1;
			else if(is==-1){
				printf("%s %02d\n",cal[i].name,month);
				double money=0;
				do{
					int fen;
					double sum;
					getsum(cal[next-1],cal[next],fen,sum);
					money+=sum;
					printf("%02d:%02d:%02d %02d:%02d:%02d %d %.2f\n",cal[next-1].d,cal[next-1].h,cal[next-1].m,cal[next].d,cal[next].h,cal[next].m,fen,sum);
					next++;
					while(next<n&&strcmp(cal[next].name,cal[i].name)==0&&!(cal[next].of==1&&cal[next-1].of==0)) next++;
				}while(next<n&&strcmp(cal[next].name,cal[i].name)==0);
				printf("Total amount: $%.2f\n",money);
				break;
			}
			next++;
		}
		i=next;
	}
}

大佬们可否看下我这个错在哪,我在本地运行的答案正确,到上面就不行了

发表于 2020-02-20 11:41:31 回复(0)
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>

using namespace std;

class record {
public:
	int time[4], state;
	string id;
	string getstrtime() {
		string s;
		for (int i = 1; i < 4; i++) {
			if (time[i] == 0) {
				s += "00";
			}
			else if (time[i] < 10) {
				s += "0";
				s += to_string(time[i]);
			}
			else {
				s += to_string(time[i]);
			}
			s += ":";
		}
		s=s.substr(0,s.size()-1);
		return s;
	}
};
bool cmp(record* r1, record* r2) {
	if (r1->id != r2 -> id) {
		return r1->id < r2->id;
	}
	else for (int i = 0; i < 4; i++) {
		if (r1->time[i] != r2->time[i]) {
			return r1->time[i] < r2->time[i];
		}
	}
	return r1->state < r2->state;
}
vector<record*> getAvlCall(vector<record*>vr) {
	vector<record*>avl;
	int state = 0;
	record* r1 = vr.front(); vr.erase(vr.begin());
	while (vr.empty() == false) {
		record* r2 = vr.front(); vr.erase(vr.begin());
		if (r2->id == r1->id) {
			state = 2;
			if (r2->state == 1 && r1->state == 0) {
				avl.push_back(r1);
				avl.push_back(r2);
				if (vr.size() != 0) {
					 r1 = vr.front(); 
					vr.erase(vr.begin());
				}
			}
			else
			{
				r1 = r2;
			}
		}
		else {
			r1 = r2;
		}
	}
	return avl;
}
int sumtime(record* r1, record* r2) {
	int m1, m2;
	m1 = r1->time[3] + r1->time[2] * 60 +r1-> time[1] * 60 * 24;
	m2 = r2->time[3] + r2->time[2] * 60 +r2-> time[1] * 60 * 24;
	return m2 - m1;

}
int rate[24];
int getcost(record* r11, record* r22) {
	int sum = 0;
	int h1, h2,h3;
	record r1 = *r11, r2 = *r22;
	h1 = r1.time[2] + r1.time[1] * 24;
	h2 = r2.time[2] + r2.time[1] * 24;
	h3 = h2 - h1;
	if (h3 > 0) {

		sum = rate[r1.time[2]] * (60 - r1.time[3]);
		sum += rate[r2.time[2]] * r2.time[3];
	}
	else if(h3==0) {
		sum += rate[r1.time[2]] * (r2.time[3] - r1.time[3]);
	}
	r1.time[2]++;
	for (int i = 0; i < h3-1; i++) {
		sum += rate[r1.time[2] % 24] * 60;
		r1.time[2]++;
		
	}
	return sum;
}
void shuchu(vector<record*>vr) {
	string name = "";
	int sum = 0, tolcost=0;
	float tolcostf=0.0;
	map<string, int>mp ;
	int state=0;
	for (int i = 0; i < vr.size(); i++) {
		record* r1 = vr[i];
		if (mp.find(r1->id) == mp.end()) {
			mp[r1->id] = 1;
		}
		else {
			mp[r1->id]++;
		}
	

	}
	auto it = mp.begin();
	while (it != mp.end()) {
		(*it).second /= 2;
		it++;
	}
	while (vr.size() != 0) {
		record* r1 = vr.front(); vr.erase(vr.begin());
		record* r2 = vr.front(); vr.erase(vr.begin());
		if (r1->id != name) {
			cout << r1->id << " ";
			printf("%02d\n", r1->time[0]);
			
			name = r1->id;
		}
		cout << r1->getstrtime() << " " << r2->getstrtime();
		cout <<" "<< sumtime(r1, r2)<<" ";
		float cost = getcost(r1, r2);
		printf("$%.2f\n", cost/ 100);
		tolcostf += cost;

		mp[r1->id]--;
		if (mp[r1->id] == 0) {
			printf("Total amount: $%.2f\n", tolcostf / 100); tolcostf = 0;
		}

	}
}
void PhoneBill() {
	int n;
	vector < record * > vr;
	for (int i = 0; i < 24; i++) {
		cin >> rate[i];
	}
	cin >> n;
	while (n--) {
		record *r=new record();
		string time, state;
		cin >> r->id >> time >> state;
		if (state == "on-line") {
			r->state = 0;
		}
		else {
			r->state = 1;
		}
		for (int i = 0; i < 4; i++) {

			string tsubs = time.substr(0, time.find(':'));
			r->time[i]=stoi(tsubs);
			time.erase(0, time.find(':') + 1);
		}
		vr.push_back(r);
	}
	sort(vr.begin(), vr.end(), cmp);
	vr = getAvlCall(vr);
	shuchu(vr);
}
int main()
{
	PhoneBill();
	return 0;
}

发表于 2020-02-01 18:24:13 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by CunNH3
 * on 2019/10/6 at 12:41.
 */

public class PhoneBills {
    public static double billFromZero(Record record, int[] rate) {
        double res = rate[record.hour] * record.minute + rate[24] * 60 * record.day;
        for (int i = 0; i < record.hour; i++) {
            res += rate[i] * 60;
        }
        return res / 100;
    }


    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int[] rate = new int[25];
        String[] l = bf.readLine().split(" ");
        for (int i = 0; i < 24; i++) {
            rate[i] = Integer.valueOf(l[i]);
            rate[24] += rate[i];
        }
        l = bf.readLine().split(" ");
        int N = Integer.valueOf(l[0]);

        Record[] records = new Record[N];

        for (int i = 0; i < N; i++) {
            l = bf.readLine().split(" ");
            records[i] = new Record(l);
        }

        Arrays.sort(records);
        Map<String, ArrayList<Record>> res = new HashMap<>();

        for (int i = 1; i < N; i++) {
            if (records[i - 1].name.equals(records[i].name) == true &&
                    records[i - 1].status == 0 && records[i].status == 1) {
                if (!res.containsKey(records[i].name)) {
                    ArrayList<Record> s = new ArrayList<>();
                    s.add(records[i - 1]);
                    s.add(records[i]);
                    res.put(records[i].name, s);
                } else {
                    ArrayList t = res.get(records[i].name);
                    t.add(records[i - 1]);
                    t.add(records[i]);

                }
            }
        }
        Object[] names = res.keySet().toArray();
        Arrays.sort(names);


        for (int i = 0; i < names.length; i++) {
            double total = 0.0;
            ArrayList<Record> t = res.get(names[i]);
            System.out.println(names[i] + String.format(" %02d", t.get(0).month));
            for (int j = 0; j < t.size() - 1; j += 2) {
                Record o = t.get(j);
                Record f = t.get(j + 1);
                System.out.printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n", o.day, o.hour, o.minute, f.day, f.hour, f.minute, f.time - o.time, billFromZero(f, rate) - billFromZero(o, rate));
                total += (billFromZero(f, rate) - billFromZero(o, rate));
            }
            System.out.printf("Total amount: $%.2f\n", total);
        }

    }
}

class Record implements Comparable<Record> {
    public String name;
    int month, day, hour, minute, status, time;

    public Record(String[] l) {
        this.name = l[0];
        String[] t = l[1].split(":");
        this.month = Integer.valueOf(t[0]);
        this.day = Integer.valueOf(t[1]);
        this.hour = Integer.valueOf(t[2]);
        this.minute = Integer.valueOf(t[3]);
        this.status = l[2].equals("on-line") ? 0 : 1;
        this.time = day * 24 * 60 + hour * 60 + minute;
    }

    @Override
    public int compareTo(Record o) {
        return (!this.name.equals(o.name)) ? this.name.compareTo(o.name) : this.time - o.time;
    }
}

牛客通过了,但是PAT没有,蛋疼
发表于 2019-10-06 15:36:51 回复(0)
def getp(a,b):
    bill = 0
    if b[0]-a[0]>0:
        bill+=(b[0]-a[0])*billday
    if b[1]>=a[1]:
        for i in range(a[1],b[1]):
            bill+=billst[i]*60/100
        bill=bill-billst[a[1]]*a[2]/100+billst[b[1]]*b[2]/100
    else:
        for i in range(0,a[1]):
            bill-=billst[i]*60/100
        for i in range(0,b[1]):
            bill+=billst[i]*60/100
        bill=bill-billst[a[1]]*a[2]/100+billst[b[1]]*b[2]/100
    return bill
         
namelst = {}
name = []
billst = list(map(int,input().split()))
billday = 0
for i in billst:
    billday+=i*60/100
n = input()
n = int(n)
for i in range(n):
    tem = input().split()
    tein = list(map(int,tem[1].split(":")))
    tein.append(tem[2])
    if tem[0] not in namelst:
        namelst[tem[0]] = [tein]
        name.append(tem[0])
    else:
        namelst[tem[0]].append(tein)
name.sort()
mon = "%02d"%(namelst[name[0]][0][0])
for i in name:
    s1 = i+" "+mon
    namelst[i].sort(key=lambda x:(x[1],x[2],x[3]))
    k1,k2 = 0,1
    total=0
    d = []
    while k1<len(namelst[i])-1:
        if namelst[i][k1][4]=='on-line' and namelst[i][k2][4]=='off-line':
            start,end = namelst[i][k1][1:4],namelst[i][k2][1:4]
            startout,endout = [],[]
            for l in start:
                startout.append("%02d"%l)
            for l in end:
                endout.append("%02d"%l)
            s2=":".join(startout)+" "+":".join(endout)+" "
            minute = end[0]*60*24+end[1]*60+end[2]-(start[0]*60*24+start[1]*60+start[2])
            s2+=str(minute)+" "
            price = getp(start,end)
            s2+="$"+"%.2f"%price
            total+=price
            k1+=1
            k2=k1+1
            d.append(s2)
        else:
            k1+=1
            k2=k1+1
    s3="Total amount: $"+"%.2f"%total
    if d:
        print(s1)
        for q in d:
            print(q)
        print(s3)
这道题目就是理解题,实现过程并不复杂
发表于 2018-12-14 21:43:02 回复(0)
#include <stdio.h>
#include <string.h>
#include <stdlib.h> 

typedef struct record{
    char name[25];
    char time[12];
    int status;
}record;

int count(int t1,int t2,int a[]){

    int i,j,d;
    int ans=0;

    for(i=0;i<24;i++)    ans+=a[i]*60;
    d=(t2-t1)/(24*60);
    ans*=d;
    t2=t2-d*24*60;
    i=t1/60;
    j=t2/60;
    if(i!=j){
        ans+=a[i]*((i+1)*60-t1)+a[j%24]*(t2-j*60);
        for(d=i+1;d<j;d++)    ans+=60*a[d%24];
    }
    else{
        ans+=a[i]*(t2-t1);
    } 

    return ans;
}

void sort(record a[],int l,int r){
    int i,j;
    record val=a[l];
    i=l;
    j=r;
    if(l<r){
        while(i<j){
            for(;j>i;j--)
                if(strcmp(a[j].name,val.name)<0 ||
                    (strcmp(a[j].name,val.name)==0 && strcmp(a[j].time,val.time)<0)){
                    a[i]=a[j];
                    break;
                }
            for(;i<j;i++)
                if(strcmp(a[i].name,val.name)>0 ||
                    (strcmp(a[i].name,val.name)==0 && strcmp(a[i].time,val.time)>0)){
                    a[j]=a[i];
                    break;
                }
        }
        a[i]=val;
        sort(a,l,i-1);
        sort(a,i+1,r);
    }
}

int main(){

    int i,j,n;
    int cost[24];
    char s[25];
    record *r;
    int k,total,ans,bo1,bo2;
    int t,m1,m2;

    for(i=0;i<24;i++)    scanf("%d",&cost[i]);
    scanf("%d",&n);
    r=(record*)malloc(sizeof(record)*n);
    for(i=0;i<n;i++){
        scanf("%s %s %s",r[i].name,r[i].time,s);
        if(s[1]=='n')    r[i].status=1;
        else r[i].status=0;
    }

    sort(r,0,n-1);



    i=0;bo2=0;
    while(i<n){
        strcpy(s,r[i].name);
        k=i;
        bo1=0;
        total=0;
        while(strcmp(s,r[k].name)==0)    k++;
        while(i<k){
            while((r[i].status)==0 && i<k)    i++;
            if(i<(k-1)){
                if(r[i+1].status==0){
                    if(!bo1){
                        if(bo2)    printf("\n");
                        printf("%s %c%c\n",s,r[i].time[0],r[i].time[1]);
                        bo1=1;
                    }    
                    for(j=3;j<11;j++)
                        printf("%c",r[i].time[j]);
                    printf(" ");
                    for(j=3;j<11;j++)
                        printf("%c",r[i+1].time[j]);
                    printf(" ");
        
                    t=r[i].time[6]-'0';
                    t=t*10+r[i].time[7]-'0';
                    m1=t*60;
                    t=r[i].time[9]-'0';
                    t=t*10+r[i].time[10]-'0';
                    m1+=t;

                    t=r[i+1].time[6]-'0';
                    t=t*10+r[i+1].time[7]-'0';
                    m2=t*60;
                    t=r[i+1].time[9]-'0';
                    t=t*10+r[i+1].time[10]-'0';
                    m2+=t;
                    t=(r[i+1].time[3]-'0')*10+(r[i+1].time[4]-'0')-((r[i].time[3]-'0')*10+(r[i].time[4]-'0'));
                    m2+=t*24*60;
                    
                    printf("%d ",m2-m1);
                    ans=count(m1,m2,cost);

                    total+=ans;
                    printf("$%.2f\n",ans*1.0/100);
                    i=i+2;
                }
                else{
                    ++i;
                }
            }
            else{
                i=k;
                break;
            }
        }
        if(bo1){
            printf("Total amount: $%.2f",total*1.0/100);
            bo2=1;
        }
    }
    
    return 0;
}


写的很乱,有兴趣的可以看一下。计算费用的主要思路是将开始打电话的那一天作为第一天,结束电话的一天作为最后一天,然后将dd:hh:mm转化为整数minutes,然后进行计算
发表于 2018-07-12 00:58:40 回复(0)
这个题怎么这么难读懂呢,太难理解了,我考试的时候碰见这个题估计就绝望了;)他们要匹配时间段到底是怎么匹配的。。。
发表于 2018-03-06 19:36:44 回复(0)

问题信息

难度:
24条回答 5602浏览

热门推荐

通过挑战的用户