PAT甲级1016. Phone Bills
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的更多相关文章
- PAT 甲级 1016 Phone Bills (25 分) (结构体排序,模拟题,巧妙算时间,坑点太多,debug了好久)
1016 Phone Bills (25 分) A long-distance telephone company charges its customers by the following r ...
- pat甲级1016
1016 Phone Bills (25)(25 分) A long-distance telephone company charges its customers by the following ...
- PAT A 1016. Phone Bills (25)【模拟】
题目:https://www.patest.cn/contests/pat-a-practise/1016 思路:用结构体存储,按照名字和日期排序,然后先判断是否有效,然后输出,时间加减直接暴力即可 ...
- PAT甲级——A1016 Phone Bills
A long-distance telephone company charges its customers by the following rules: Making a long-distan ...
- PAT 1016 Phone Bills[转载]
1016 Phone Bills (25)(25 分)提问 A long-distance telephone company charges its customers by the followi ...
- PAT 1016 Phone Bills(模拟)
1016. Phone Bills (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A long-di ...
- 1016. Phone Bills (25)——PAT (Advanced Level) Practise
题目信息: 1016. Phone Bills (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A l ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 【转载】【PAT】PAT甲级题型分类整理
最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...
随机推荐
- Machine Learning系列--维特比算法
维特比算法(Viterbi algorithm)是在一个用途非常广的算法,本科学通信的时候已经听过这个算法,最近在看 HMM(Hidden Markov model) 的时候也看到了这个算法.于是决定 ...
- AD服务器安装文档
Windows Server 2008 R2 AD服务器搭建 1. AD服务器简介 应用到: Windows Server 2003, Windows Server 2003 R2, Windows ...
- CGIC简明教程(转摘)
CGIC简明教程 本系列的目的是演示如何使用C语言的CGI库“CGIC”完成Web开发的各种要求. ********************************* 基础知识 1 ...
- CVE-2010-3971 CSS内存破坏漏洞分析
看了仙果版主的议题演讲,其中提到cve-2010-3971是一个浏览器漏洞利用中的里程碑.于是找来POC,尝试分析一下. 1.漏洞重现 XP SP3+ie6.0环境 poc如下: poc.htm &l ...
- [实战]MVC5+EF6+MySql企业网盘实战(5)——ajax方式注册
写在前面 今天贴合到实际的客户需求仔细的想了下,其实在userInfo这个类里面很多字段都不是必须的.有很多的事业单位根本就不能上网,填写的邮箱也是exchange的,个人的详细信息都在ad里面可以取 ...
- spring_150906_sqlmapclientdaosupport_getSqlMapClientTemplate
添加到ibatis相关jar包! 实体类: package com.spring.model; public class DogPet { private int id; private String ...
- 【笔试题】怎样将 GB2312 编码的字符串转换为 ISO-8859-1 编码的字符串?
笔试题 怎样将 GB2312 编码的字符串转换为 ISO-8859-1 编码的字符串? import java.io.UnsupportedEncodingException; public clas ...
- 记录自己在 cmd 中执行 jar 文件遇到的一些错误
记录自己在 cmd 中执行 jar 文件遇到的一些错误 场景: 请求接口,解析接口返回的 JSON 字符串并插入到我们的数据库里面. 情况: 项目在 eclipse 中正常运行,打成 jar 包后在 ...
- mysql打印输出转csv格式
1. mysql打印输出放在input.csv中 2. 执行该文件 <?php $str = file_get_contents("./input.csv"); $str = ...
- .htaccess文件
前言 看了几篇文章,发现自己对于如何维护普通的服务器安全完全不会,先从简单的.htaccess来研究吧 .htaccess文件的作用,就是更改httpd.ini文件中的配置,但作用范围仅限当前文件夹 ...