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

给出一天24小时内,每个小时内,每分钟的通话费用
给出n个记录,on-line表示通话的开始,off-line表示通话的结束
如果on-line/off-line没有对应的另一个,忽略即可

先按人名排序,名称一样的按时间排序,这里时间统一按分钟来算,即一天有24*60分钟,那么01:01:05就是0*24*60+1*60+5
然后找出彼此配对的on-line和off-line,存入phone数组
然后接下来就是求出每个通话时间的费用即可

(主要是如何计算出费用比较细节麻烦一点,推荐做一下)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define ONLINE 0
#define OFFLINE 1
using namespace std;
const int maxn=+;
const int DAYMINUTE=*;
const int HOURMINUTE=;
int toll[];
int n;
struct Record{
char name[];
int time;
int month;
int day;
int hour;
int minute;
int mark;
bool operator<(const Record tmp)const{
if(strcmp(name,tmp.name)==)
return time<tmp.time;
else if(strcmp(name,tmp.name)<)
return true;
else
return false;
}
}record[maxn]; struct Phone{
char name[];
int month;
int d1,h1,m1;
int d2,h2,m2;
int time1,time2;
}phone[maxn];
int cnt=;
/**
求出第i个配对的通话时间费用
*/
int cal(int i){
int sum=;
int start=phone[i].time1; //起始时间
int ends=phone[i].time2; //结束时间
int tmp=; //统计每个小时的时间段内,通话的分钟
int idx; //对应时间段toll的索引
for(int t=start;t<=ends;t++){
if(t%==){
if(t%DAYMINUTE==)
idx=(DAYMINUTE-)/; //如果模为0,就应该是DAYMINUTE
else
idx=(t%DAYMINUTE-)/; //因为可能会有连续通话了好几天,所以得取模一下
sum+=tmp*toll[idx]; //通话的分钟*该时间段的费用
tmp=; //为了方便起见,像01:06:00这种整时的,算作下一小时的
}
else
tmp++;
}
if(tmp){
//比如说1:08:03,由于1:08:00的时候算作8-9之间的,实际上统计的tmp=4,所以要-1
sum+=(tmp-)*toll[(ends%DAYMINUTE)/];
}
return sum;
}
int main()
{
char word[];
for(int i=;i<;i++)
scanf("%d",&toll[i]);
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%s %d:%d:%d:%d %s",record[i].name,&record[i].month,&record[i].day,&record[i].hour,&record[i].minute,word);
record[i].time=(record[i].day-)*DAYMINUTE+record[i].hour*HOURMINUTE+record[i].minute;
if(word[]=='n')
record[i].mark=ONLINE;
else
record[i].mark=OFFLINE;
}
sort(record,record+n);
int last=-; //之前最近的一个on-line的索引
//找出配对的on-line和off-line,存入phone数组,方便后序处理
for(int i=;i<n;i++){
if(record[i].mark==ONLINE){
last=i;
}
if(record[i].mark==OFFLINE && last!=-){
if(strcmp(record[i].name,record[last].name)==){
strcpy(phone[cnt].name,record[i].name);
phone[cnt].month=record[i].month;
phone[cnt].d1=record[last].day;
phone[cnt].h1=record[last].hour;
phone[cnt].m1=record[last].minute;
phone[cnt].time1=record[last].time;
phone[cnt].d2=record[i].day;
phone[cnt].h2=record[i].hour;
phone[cnt].m2=record[i].minute;
phone[cnt].time2=record[i].time;
cnt++;
last=-;
}
}
}
int tot=,sum=;
for(int i=;i<cnt;i++){
if(i==){
printf("%s %02d\n",phone[i].name,phone[i].month);
sum=cal(i);
tot+=sum;
int len=phone[i].time2-phone[i].time1;
double cost=sum*1.0/;
printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2lf\n",phone[i].d1,phone[i].h1,phone[i].m1,phone[i].d2,phone[i].h2,phone[i].m2,len,sum*1.0/);
}
else{
if(strcmp(phone[i].name,phone[i-].name)!=){
printf("Total amount: $%.2lf\n",tot*1.0/);
printf("%s %02d\n",phone[i].name,phone[i].month);
tot=;
}
sum=cal(i);
tot+=sum;
int len=phone[i].time2-phone[i].time1;
int cost=sum*1.0/;
printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2lf\n",phone[i].d1,phone[i].h1,phone[i].m1,phone[i].d2,phone[i].h2,phone[i].m2,len,sum*1.0/);
}
}
if(tot){
printf("Total amount: $%.2lf\n",tot*1.0/);
}
return ;
}

PAT甲题题解-1016. Phone Bills (25)-模拟、排序的更多相关文章

  1. PAT甲题题解-1028. List Sorting (25)-水排序

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

  2. PAT甲题题解-1051. Pop Sequence (25)-堆栈

    将1~n压入最多为m元素的栈 给出k个出栈序列,问你是否能够实现. 能输出YES 否则NO 模拟一遍即可,水题. #include <iostream> #include <cstd ...

  3. PAT甲题题解-1059. Prime Factors (25)-素数筛选法

    用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...

  4. PAT甲题题解-1101. Quick Sort (25)-大水题

    快速排序有一个特点,就是在排序过程中,我们会从序列找一个pivot,它前面的都小于它,它后面的都大于它.题目给你n个数的序列,让你找出适合这个序列的pivot有多少个并且输出来. 大水题,正循环和倒着 ...

  5. PAT甲题题解-1117. Eddington Number(25)-(大么个大水题~)

    如题,大水题...贴个代码完事,就这么任性~~ #include <iostream> #include <cstdio> #include <algorithm> ...

  6. PAT甲题题解-1130. Infix Expression (25)-中序遍历

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

  7. PAT甲题题解-1129. Recommendation System (25)-排序

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

  8. PAT甲题题解-1021. Deepest Root (25)-dfs+并查集

    dfs求最大层数并查集求连通个数 #include <iostream> #include <cstdio> #include <algorithm> #inclu ...

  9. PAT甲题题解-1024. Palindromic Number (25)-大数运算

    大数据加法给一个数num和最大迭代数k每次num=num+num的倒序,判断此时的num是否是回文数字,是则输出此时的数字和迭代次数如果k次结束还没找到回文数字,输出此时的数字和k 如果num一开始是 ...

随机推荐

  1. tidb导入大量数据报错:statement count 5001 exceeds the transaction limitation, autocommit = false

    这是Tidb数据库事务提交数量达到上限的一种报错:因为tidb是分布式的数据库,tikv使用了底层的强一致性协议.这是分布式数据库必然遇到的一个问题,我们可以调整这个值:在tidb的配置文件里面“st ...

  2. yum 私有仓库

    参考地址:https://blog.oldboyedu.com/autodeploy-yum YUM主要用于自动安装.升级rpm软件包,它能自动查找并解决rpm包之间的依赖关系.要成功的使用YUM工具 ...

  3. November 12th, 2017 Week 46th Sunday

    I love you not for who you are, but for who I am with you. 我爱你不是因为你是谁,而是因为跟你在一起,我是谁. I enjoy the fee ...

  4. Beta 冲刺 (7/7)

    Beta 冲刺 (7/7) 队名:洛基小队 峻雄(组长) 已完成:人物释放技能部分的实现 后两天计划:整合脚本,测试内容 剩余任务:整合各部分脚本 困难:尽快完善整合出β版的内容 非易 已完成:商店功 ...

  5. 团队作业——Alpha冲刺 5/12

    团队作业--Alpha冲刺 冲刺任务安排 杨光海天 今日任务:编辑界面完成部分内容,学习了下拉菜单控件的建立,完善界面标题内容,以及交互. 明日任务:继续完善编辑界面,学习使用gallery,着手配图 ...

  6. BZOJ 1208 宠物收养所 set+二分

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1208 题目大意: 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠 ...

  7. php请求API接口方法

    thinkphp下直接放入公共函数即可. /** * 通过URL获取页面信息 * @param string $url 地址 * @return string 返回页面信息 */ function g ...

  8. tusen 刷题

    //1.single number和变体 //2.lru lfu 3.给一个正整数集合,求一个和最大且能被3整除的子集.Follow up: 如果集合里有正有负 4.leetcode200-numbe ...

  9. Python2.7-ConfigParser

    ConfigParser模块,用于读写配置文件,配置文件是由各个 section 组成的,每个部分都有一个 [section] 头指示,后面紧跟这部分里的配置信息,一般为 name: value 或 ...

  10. jsp二(指令)

    一.jsp动作标签: 1)<jsp:forward> 请求转发 相当于之前的request.getRequestDispatcher(..).forward(..); <!--jsp ...