【1016】Phone Bills (25 分)

举报
野猪佩奇996 发表于 2022/01/22 23:45:36 2022/01/22
【摘要】 #include<iostream>#include<stdio.h>#include<stdlib.h>#include<math.h>#include<string.h>#include<algorithm> #include<map>#inclu...

  
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<math.h>
  5. #include<string.h>
  6. #include<algorithm>
  7. #include<map>
  8. #include<vector>
  9. #include<queue>
  10. using namespace std;
  11. //对所有记录排序,对每个用户判断其是否存在有效通话记录,如果存在有效通话记录,
  12. //则输出所有有效通话记录并计费
  13. const int maxn=1010;
  14. int toll[25];//资费
  15. struct Record{
  16. char name[25]; //姓名
  17. int month,dd,hh,mm; //月份、日、时、分
  18. bool status; //status==true表示该记录为on-line,否则为off-line
  19. }rec[maxn],temp;
  20. bool cmp(Record a,Record b){
  21. int s=strcmp(a.name,b.name);
  22. if(s != 0) return s<0; //优先按姓名字典序从小大排序
  23. else if(a.month !=b.month) return a.month<b.month;//按月份从小到大排序
  24. else if(a.dd != b.dd) return a.dd<b.dd; //按照日期从小到大排序
  25. else if(a.hh != b.hh) return a.hh<b.hh; //按小时从小到大排序
  26. else return a.mm<b.mm; //按分钟从小到大排序
  27. }
  28. //计算时间和金钱
  29. void get_ans(int on,int off,int& time,int& money){
  30. temp=rec[on];
  31. while(temp.dd<rec[off].dd || temp.hh <rec[off].hh || temp.mm <rec[off].mm){
  32. time++; //该次记录总时间加1min
  33. money +=toll[temp.hh]; //花费增加toll[temp.hh]
  34. temp.mm++; //当前时间加1min
  35. if(temp.mm>=60){ //当前分钟数到达60
  36. temp.mm=0; //进入下一小时
  37. temp.hh++;
  38. }
  39. if(temp.hh>=24){ //当前小时到达24
  40. temp.hh=0;//进入下一天
  41. temp.dd++; //日子数加1
  42. }
  43. }
  44. }
  45. int main(){
  46. for(int i=0;i<24;i++){
  47. scanf("%d",&toll[i]); //资费
  48. }
  49. int n;
  50. scanf("%d",&n); //记录数
  51. char line[10]; //临时存放on-line或off-line的输入
  52. for(int i=0;i<n;i++){
  53. scanf("%s",rec[i].name);
  54. scanf("%d:%d:%d:%d",&rec[i].month,&rec[i].dd,&rec[i].hh,&rec[i].mm);
  55. //年份,日,时,分
  56. scanf("%s",line);
  57. if(strcmp(line,"on-line") == 0){
  58. rec[i].status=true; //如果是on-line,则令status为true
  59. }else{
  60. rec[i].status=false; //如果是off-line,则令status为false
  61. }
  62. }
  63. sort(rec,rec+n,cmp); //排序
  64. int on=0,off,next; //on和off为配对的两条记录,next为下一个用户
  65. while(on < n){ //每次循环处理单个用户的所有记录
  66. int needPrint=0; //needPrint表示该用户是否需要输出
  67. next=on; //从当前位置开始寻找下一个用户
  68. while(next<n && strcmp(rec[next].name , rec[on].name )==0){
  69. if(needPrint == 0 && rec[next].status == true){
  70. needPrint=1; //找到on,置needPrint为1
  71. }else if(needPrint == 1 && rec[next].status ==false){
  72. needPrint=2; //在on之后如果找到off,置needPrint为2
  73. }
  74. next++; //next自增,知道找到不同名字,即下一个用户
  75. }
  76. if(needPrint <2){ //没有找到配对的on-off
  77. on=next;
  78. continue;
  79. }
  80. int AllMoney=0 ;//总共花费的钱
  81. printf("%s %02d\n",rec[on].name , rec[on].month);
  82. while(on < next){ //寻找该用户的所有配对
  83. while(on<next-1
  84. && !(rec[on].status == true&&rec[on+1].status==false)){
  85. on++;//知道找到连续的on-line和off-line
  86. }
  87. off=on+1; //off必须是on的下一个
  88. if(off ==next){ //已经输出完毕所有的配对的on-line和off-line
  89. on=next;
  90. break;
  91. }
  92. printf("%02d:%02d:%02d ",rec[on].dd,rec[on].hh, rec[on].mm);
  93. printf("%02d:%02d:%02d ",rec[off].dd,rec[off].hh, rec[off].mm);
  94. int time=0,money=0; //时间、单次记录花费的钱
  95. get_ans(on,off,time,money); //计算on到off内的时间和金钱
  96. AllMoney+=money; //总金额加上该次记录的钱
  97. printf("%d $%.2f\n",time,money/100.0);
  98. on=off+1 ;//完成一个配对,从off+1开始找下一对
  99. }
  100. printf("Total amount: $%.2f\n",AllMoney/100.0);
  101. }
  102. system("pause");
  103. return 0;
  104. }

 

文章来源: andyguo.blog.csdn.net,作者:山顶夕景,版权归原作者所有,如需转载,请联系作者。

原文链接:andyguo.blog.csdn.net/article/details/100006141

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。