博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~
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. November 06th, 2017 Week 45th Monday

    The education of a man is never completed until he dies. 一个人的学习之路,到死才结束. Being a life-long learning ...

  2. spark-机器学习实践-K近邻应用实践一

    K近邻应用-异常检测应用 原理: 根据数据样本进行KMeans机器学习模型的建立,获取簇心点,以簇为单位,离簇心最远的第五个点的距离为阈值,大于这个值的为异常点,即获得数据异常. 如图:

  3. css常见知识点

    1.内核区分 希望某一个浏览器能一统江湖 -ms-transform:rotate(7deg); //-ms代表ie内核识别码 -moz-transform:rotate(7deg); //-moz代 ...

  4. [Android自动化] 在 pip-9.0.1 版本情况下安装 uiautomator2 报错的解决办法

    1.在命令窗口中使用命令: pip install uiautomator2 时报 pip 版本过低,需要先升级 pip 版本,理论上会按照提示进行升级 pip 操作,但执行升级命令时到最后却还是报错 ...

  5. Math.min() / Math.max() 使用方法

    首先弄懂apply 和 call 都是js函数自带的方法.区别如下: apply和call的用法只有一个地方不一样,除此之外,其他地方基本一模一样 1. a.call(b,arg1,arg2…) 2. ...

  6. 性能问题案例01——sybase数据库内存问题

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xuepiaohan2006/article/details/30064399     近期现场反馈问 ...

  7. Azkaban学习之路 (一)Azkaban的基础介绍

    一.为什么需要工作流调度器 1.一个完整的数据分析系统通常都是由大量任务单元组成: shell 脚本程序,java 程序,mapreduce 程序.hive 脚本等 2.各任务单元之间存在时间先后及前 ...

  8. Maven搭建私服

    为什么要搭建私服?搭建私服有什么好处? 以我最近技术调研和相关的使用为起点概述: 首先说明,为什么要搭建私服? 搭建私服的目的是,通常企业项目开发,特别是使用maven作为项目管理,现在非常流行使用m ...

  9. 最长公共子串(LCS:Longest Common Substring)

    最长公共子串(LCS:Longest Common Substring)是一个非常经典的面试题目,本人在乐视二面中被面试官问过,惨败在该题目中. 什么是最长公共子串 最长公共子串问题的基本表述为:给定 ...

  10. WebView之javascript与android交互基础加强

    一.什么是js与android交互? 通俗一点就是使用js代码调用java代码,或者使用java代码调用js代码. 二.为什么要使用js与java代码交互? 1.可以做一些js网页做本身处理不了的事情 ...