博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~
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. [LOJ 2720][BZOJ 5417][UOJ 395][NOI 2018]你的名字

    [LOJ 2720][BZOJ 5417][UOJ 395][NOI 2018]你的名字 题意 给定一个大串 \(S\) 以及 \(q\) 次询问, 每次询问给定一个串 \(T\) 和区间 \([l, ...

  2. (转)python3 urllib.request.urlopen() 错误UnicodeEncodeError: 'ascii' codec can't encode characters

    代码内容: url = 'https://movie.douban.com/j/search_subjects?type=movie'+ str(tag) + '&sort=recommend ...

  3. BZOJ4556:[TJOI\HEOI2016]字符串(后缀数组,主席树,二分,ST表)

    Description 佳媛姐姐过生日的时候,她的小伙伴从某东上买了一个生日礼物.生日礼物放在一个神奇的箱子中.箱子外边写了一个长为n的字符串s,和m个问题.佳媛姐姐必须正确回答这m个问题,才能打开箱 ...

  4. InnerClass annotations are missing corresponding EnclosingMember annotations. Such InnerClas...

    如果 你的项目中使用了注解插件 比如butterknife   升级3.1之后打包编译  出现以下错误提示 InnerClass annotations are missing correspondi ...

  5. linux 的常用命令---------第八阶段

            raid 磁盘阵列-------raid 0     raid1                              raid5      raid10 mdadm 命令常用参数 ...

  6. nmap数据流

    扫描者:1.1.1.1被扫描者:2.2.2.2 0x00 介绍 在日常工作对目标信息收集时,我们经常用到nmap这款网络探测工具和安全/端口扫描器,虽然我们关注的是结果(如目标开启了哪些危险端口,什么 ...

  7. Qt Creator无法debug,报错:The selected debugger may be inappropriate for the inferior. Examining symbols and setting breakpoints by file name and line number may fail. The inferior is in the Portable ...

    看到这个报错我是绝望的 解决:下载windows sdk  win10 sdk 只安装Debugging Tools for Windows 打开 工具-选项-Kits 安装sdk成功后我们可以看到 ...

  8. Python2.7-bisect

    bisect 模块,对已经排好序的序列进行筛选,添加新元素,效率高,不用在插入新元素后重新排序,可以快速找到小于指定值的位置 个人想法:与 heapq 的堆可以较好的结合使用 模块方法:bisect_ ...

  9. k-center问题-学习

    k-center问题: In graph theory, the metric k-center or metric facility location problem is a combinator ...

  10. Foxmail添加gmail密码错误

    想在foxmail上添加gmail时一直报密码错误,找了一圈发现是开启了两步验证,需要用应用专用密码才可以登录,生成应用专用密码的地址如下: https://security.google.com/s ...