PAT甲级1016. Phone Bills

题意:

长途电话公司按以下规定向客户收取费用:

长途电话费用每分钟一定数量,具体取决于通话时间。当客户开始连接长途电话时,将记录时间,并且客户挂断电话时也是如此。

每个日历月份,每分钟都会向客户发送一个帐单(按照一天中的时间确定)。您的工作是为每个月准备账单,给出一组电话记录。

输入:

每个输入文件包含一个测试用例。每个案例有两部分:费率结构和电话记录。

费率结构包括一个24个非负整数表示从00:00至01:00的收费(分/分),01:00至02:00之间的收费,以及当天每小时的费用。

下一行包含正数N(<= 1000),后跟N行记录。

每个电话记录包括客户名称(最多20个字符的空格字符串),时间和日期(mm:dd:hh:mm)和单词“在线”或“离线” 。

对于每个测试用例,所有日期将在一个月内。每个“

线“记录与同一客户的时间顺序的下一条记录配对,只要它是一个”离线“记录,任何不与”离线“记录配对的”在线“记录将被忽略,”离线“记录未与”在线“记录配对,保证在输入中至少有一个呼叫配对良好。

您可以假设同一客户没有两个记录有相同的时间。使用24小时制记录时间。

输出:

对于每个测试用例,您必须为每个客户打印一个电话费。

票据必须按客户姓名的字母顺序打印。对于每个客户,

首先按照示例显示的格式,一行列出客户的名称和帐单的月份。然后,对于每个呼叫的时间段,在一行中打印开始和结束时间和日期(dd:hh:mm),持续时间(分钟)和通话费用。呼叫必须按时间顺序列出。最后,

以样品显示的格式打印月份的总费用。

思路:

这题很烦。

把所有的通话记录排序。只要第i个是online i + 1是offline就是有效的。

有个地方特别坑,就是一个用户要是没有任何有效的通话记录的话,就什么都不输出。连名字,total = 0都不输出。哎。不消费的话,什么都不给你输出。万恶的资本主义。别的地方注意一下细节就好了。

ac代码:

C++

// pat1016.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h" #include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
#include<unordered_map>
#include<map> using namespace std; int charge[24]; struct bill
{
string id;
vector<pair<int,string>> line;
vector<pair<string, string>> vaild;
vector<int> costtime;
vector<double> cost;
}; static bool cmp(pair<int,string> a, pair<int, string> b)
{
int amonth, adata, ahour, aminute;
int bmonth, bdata, bhour, bminute;
amonth = stoi(a.second.substr(0, 2));
adata = stoi(a.second.substr(3, 2));
ahour = stoi(a.second.substr(6, 2));
aminute = stoi(a.second.substr(9, 2));
bmonth = stoi(b.second.substr(0, 2));
bdata = stoi(b.second.substr(3, 2));
bhour = stoi(b.second.substr(6, 2));
bminute = stoi(b.second.substr(9, 2)); if (amonth != bmonth)
{
return amonth < bmonth;
}
else if (adata != bdata)
{
return adata < bdata;
}
else if(ahour != bhour)
{
return ahour < bhour;
}
else if (aminute != bminute)
{
return aminute < bminute;
}
return true;
} int counttime(string a, string b)
{
int adata, ahour, aminute;
int bdata, bhour, bminute;
adata = stoi(a.substr(3, 2));
ahour = stoi(a.substr(6, 2));
aminute = stoi(a.substr(9, 2));
bdata = stoi(b.substr(3, 2));
bhour = stoi(b.substr(6, 2));
bminute = stoi(b.substr(9, 2)); int res;
res = (bdata - adata) * 24 * 60 + (bhour - ahour) * 60 + (bminute - aminute);
return res;
} double count(string a, string b)
{
int adata, ahour, aminute;
int bdata, bhour, bminute;
adata = stoi(a.substr(3, 2));
ahour = stoi(a.substr(6, 2));
aminute = stoi(a.substr(9, 2));
bdata = stoi(b.substr(3, 2));
bhour = stoi(b.substr(6, 2));
bminute = stoi(b.substr(9, 2)); double res = 0;
int j;
for (int i = adata; i <= bdata; i++)
{
if (i < bdata)
{
for (j = ahour; j < 24; j++)
{
res += (60 - aminute) * charge[j];
aminute = 0;
}
ahour = 0;
}
else if (i == bdata)
{
for (j = ahour; j <= bhour; j++)
{
if (j < bhour)
{
res += (60 - aminute) * charge[j];
aminute = 0;
}
else if (j == bhour)
{
res += (bminute - aminute) * charge[j];
break;
}
}
}
}
return res / 100;
} void findvaild(bill& b)
{
sort(b.line.begin(), b.line.end(), cmp);
int len = b.line.size();
for (int i = 0; i < len - 1; i++)
{
if (b.line[i].first == 0 && b.line[i + 1].first == 1)
{
b.vaild.push_back(make_pair(b.line[i].second.substr(3), b.line[i + 1].second.substr(3)));
b.costtime.push_back(counttime(b.line[i].second, b.line[i + 1].second));
b.cost.push_back(count(b.line[i].second, b.line[i + 1].second));
i++;
}
}
} int main()
{
int n;
map<string,bill> bl;
vector<string> allid;
//收费时段记录
for (int i = 0; i < 24; i++)
cin >> charge[i]; //通话记录
string id, time, type;
string month;
cin >> n; while (n--)
{
cin >> id >> time >> type;
if (bl.find(id) == bl.end())
{
bill b;
b.id = id;
bl[id] = b;
allid.push_back(id);
} if (type == "on-line")
{
bl[id].line.push_back(make_pair(0,time));
}
else
{
bl[id].line.push_back(make_pair(1, time));
}
} month = time.substr(0, 2);
//处理记录
sort(allid.begin(), allid.end());
for (int i = 0; i < allid.size(); i++)
{
findvaild(bl[allid[i]]);
if (bl[allid[i]].vaild.size() == 0) continue;
double total = 0;
cout << bl[allid[i]].id << " " << month << endl;
for (int j = 0; j < bl[allid[i]].vaild.size(); j++)
{
total += bl[allid[i]].cost[j];
cout << bl[allid[i]].vaild[j].first << " " << bl[allid[i]].vaild[j].second << " " << bl[allid[i]].costtime[j] << " $";
printf("%.2f\n", bl[allid[i]].cost[j]);
}
printf("Total amount: $%.2f\n", total);
}
return 0;
}

PAT甲级1016. Phone Bills的更多相关文章

  1. PAT 甲级 1016 Phone Bills (25 分) (结构体排序,模拟题,巧妙算时间,坑点太多,debug了好久)

    1016 Phone Bills (25 分)   A long-distance telephone company charges its customers by the following r ...

  2. pat甲级1016

    1016 Phone Bills (25)(25 分) A long-distance telephone company charges its customers by the following ...

  3. PAT A 1016. Phone Bills (25)【模拟】

    题目:https://www.patest.cn/contests/pat-a-practise/1016 思路:用结构体存储,按照名字和日期排序,然后先判断是否有效,然后输出,时间加减直接暴力即可 ...

  4. PAT甲级——A1016 Phone Bills

    A long-distance telephone company charges its customers by the following rules: Making a long-distan ...

  5. PAT 1016 Phone Bills[转载]

    1016 Phone Bills (25)(25 分)提问 A long-distance telephone company charges its customers by the followi ...

  6. PAT 1016 Phone Bills(模拟)

    1016. Phone Bills (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A long-di ...

  7. 1016. Phone Bills (25)——PAT (Advanced Level) Practise

    题目信息: 1016. Phone Bills (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A l ...

  8. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  9. 【转载】【PAT】PAT甲级题型分类整理

    最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...

随机推荐

  1. 【Tomcat】tomcat中server.xml配置详解

    Tomcat Server的结构图如下: 该文件描述了如何启动Tomcat Server <Server>    <Listener />    <GlobaNaming ...

  2. git-定制属于你的log格式

    软件版本:    操作系统:ubuntu10.04     内核版本:Linux version 2.6.32-36-generic     git 版本:git version 1.7.0.4 1. ...

  3. 141.Linked List Cycle---双指针

    题目链接 题目大意:给出一个链表,判断该链表是否有环,空间复杂度最好控制在o(1) 这个题没有给测试用例,导致没太明白题目意思,看了题解,用了两种方法示例如下: 法一(借鉴):利用两个指针,一个指针步 ...

  4. Lempel-Ziv algorithm realization

    Lempel-Ziv 复杂度程序 随着人们对非线性方法的分析越加深入,他们发现,虽然关联维度和最大李雅谱诺夫指数在分析脑电时具有一定的帮助,但是它们对数据的依赖性太强,对干扰和噪 声太敏感,而且要得到 ...

  5. [ python ] 各种推导式

    各种推导式,主要使用示例演示用法 列表生成式 示例1:求0-9每个数的平方 li = [x*x for x in range(10)] print(li) # 执行结果: # [0, 1, 4, 9, ...

  6. Linux内核的三种调度策略

    一 Linux内核的三种调度策略:   1,SCHED_OTHER 分时调度策略, 2,SCHED_FIFO实时调度策略,先到先服务.一旦占用cpu则一直运行.一直运行直到有更高优先级任务到达或自己放 ...

  7. a:hover伪类在ios移动端浏览器内无效的解决方法

    a:hover 设置的样式在ios系统的浏览器内显示不出来,看来在iOS系统的移动设备中,需要在按钮元素或body/html上绑定一个touchstart事件才能激活:active状态. 方法 一: ...

  8. springcloud 出现unavailable-replicas

    springcloud 出现unavailable-replicas 原因: 1. 部分服务不可用 2. 直接使用了ip地址作为hostname application.properties # 不能 ...

  9. AC日记——Cards Sorting codeforces 830B

    Cards Sorting 思路: 线段树: 代码: #include <cstdio> #include <cstring> #include <iostream> ...

  10. Python 的十个自然语言处理工具

    原文 先mark,后续尝试. 1.NLTK NLTK 在用 Python 处理自然语言的工具中处于领先的地位.它提供了 WordNet 这种方便处理词汇资源的借口,还有分类.分词.除茎.标注.语法分析 ...