博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~
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. 高斯求积公式 matlab

    1. 分别用三点和四点Gauss-Chebyshev公式计算积分 并与准确积分值2arctan4比较误差.若用同样的三点和四点Gauss-Legendre公式计算,也给出误差比较结果. 2*atan( ...

  2. C# 利用VS自带的WSDL工具生成WebService服务类(转载)

    WebService有两种使用方式,一种是直接通过添加服务引用,另一种则是通过WSDL生成. 添加服务引用大家基本都用过,这里就不讲解了. 那么,既然有直接引用的方式,为什么还要通过WSDL生成呢? ...

  3. 关于 X509Certificate2 找到文件路径的问题

    由于微信退款功能需要用到证书,当调用 X509Certificate2 的时候,会提示找不到文件而报错. X509Certificate2 cert = new X509Certificate2(文件 ...

  4. Python2.7-filecmp

    filecmp 模块,定义了比较文件或目录的函数,比较文件只会有 True 和 False 两种结果,比较目录会返回目录下相同的文件,不同的文件,出错的文件.比较文件也可以用 difflib 模块,d ...

  5. css中的莫名空白间隙

    此时div和img直接有空白,在他们父元素设置font-size:0;就可以解决了

  6. ETH—Lwip以太网通信

    第39章     ETH—Lwip以太网通信 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/ ...

  7. SEO优化上首页之搜索引擎排名规则

    搜索引擎建立索引的网页数以万亿计,用户搜索的关键词也是海量,如果每个用户提交搜索请求后,搜索引擎都去数以万亿的索引中重新排名网页,效率将非常低下.根据2-8法则,80%是查询是集中在相同的20%内容上 ...

  8. WPF编程,C#中弹出式对话框 MessageBox 的几种用法。

    原文:WPF编程,C#中弹出式对话框 MessageBox 的几种用法. 1.MessageBox.Show("Hello~~~~"); 最简单的,只显示提示信息.   2.Mes ...

  9. Source insight 中 标题栏路径显示完整路径的方法

    在source insight 的标题栏中显示完整路径名的方法.Options -> Preferences -> Display -> Trim long path names w ...

  10. [C/C++标准库]_[初级]_[转换UTC时间到local本地时间]

    场景 1.如果有面向全球用户的网站, 一般在存储时间数据时存储的是UTC格式的时间, 这样时间是统一的, 并可以根据当地时区来进行准确的转换. 2.存储本地时间的问题就在于如果换了时区, 那么显示的时 ...