1016. Phone Bills 解析

2023-11-26 16:32
文章标签 解析 1016 phone bills

本文主要是介绍1016. Phone Bills 解析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

直接把时间化成以秒为单位 排序 匹配 计算就好。

……………………更新线…………………………

这里会遇到上一个人名on下一个人名接着off的情况需要注意下

重新写了下代码,感觉清爽了一点

#include <iostream>
#include <algorithm>
#include <climits>
#include <cstring>
#include <vector>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <set>#define MAX 1010using namespace std;double rate[24];struct node{string name;int month;int dd;int hh;int mm;int t_int;int tag;
};struct dounode{node n1;node n2;int SumMin;double sum;
};struct ansnode{string name;int month;vector <dounode> list;double sum;ansnode(){sum = 0;};
};node l[MAX];
ansnode ans[MAX]; string on = "on-line";
string off = "off-line";int n;bool cmp(node n1 ,node n2){if(n1.name != n2.name)return n1.name < n2.name;elsereturn n1.t_int < n2.t_int;
}double rateCalculate(node n1 ,node n2,int & sumh){double nowrate = 0;if(n1.dd == n2.dd){//同一天if(n1.hh == n2.hh){//同一小时nowrate += rate[n1.hh] * (n2.mm - n1.mm);sumh += (n2.mm - n1.mm);}else{//不同小时nowrate += rate[n1.hh] * (60 - n1.mm);sumh += (60 - n1.mm);nowrate += rate[n2.hh] * n2.mm;sumh += n2.mm;for(int h = n1.hh + 1; h < n2.hh;h++){nowrate += rate[h] * 60;sumh += 60;}	}}else{//不同天sumh += (60 - n1.mm);sumh += n2.mm;nowrate += rate[n1.hh] * (60 - n1.mm);for(int h = n1.hh + 1 ; h < 24; h++){nowrate += rate[h] * 60;sumh += 60;}nowrate += rate[n2.hh] * n2.mm;for(int h = 0 ;h < n2.hh;h++){nowrate += rate[h] * 60;sumh += 60;}double day = 0;for(int i = 0 ; i < 24 ;i++)day += rate[i] * 60;nowrate += day * (n2.dd - n1.dd -1);sumh += (n2.dd - n1.dd -1) * 24 * 60;}return nowrate;
}int main(){for(int i = 0; i < 24 ;i ++){scanf("%lf",&rate[i]);rate[i] /= 100;}scanf("%d",&n);for(int i = 0 ; i < n ;i++){string tag;string t_str;cin >> l[i].name >> t_str >> tag;if(tag == on)l[i].tag = 0;else l[i].tag = 1;l[i].month = (t_str[0]-'0') * 10 + (t_str[1]-'0');l[i].dd = (t_str[3]-'0') * 10 + (t_str[4]-'0');l[i].hh = (t_str[6]-'0') * 10 + (t_str[7]-'0');l[i].mm = (t_str[9]-'0') * 10 + (t_str[10]-'0');l[i].t_int = l[i].dd * 24 * 60 + l[i].hh * 60 + l[i].mm;}sort(l,l+n,cmp);int pre = 0;string nowName = " ";int no = -1;int c = 0;bool pair = false;for(int i = 0 ; i <n ;i++){if(!pair && l[i].tag == 1)continue;else if(!pair && l[i].tag == 0){pair = true;pre = i;}else if(pair && l[i].tag == 0){pre = i;}else{if(l[pre].name == l[i].name){//同一个人的电话if(nowName != l[pre].name){no++;c++;nowName = l[pre].name;ans[no].name = nowName;ans[no].month = l[pre].month;}dounode t_dou;t_dou.SumMin = 0;t_dou.sum = rateCalculate(l[pre],l[i],t_dou.SumMin);		t_dou.n1 = l[pre];t_dou.n2 = l[i];ans[no].list.push_back(t_dou);ans[no].sum += t_dou.sum;ans[no].name = nowName;				}pair = false;}}for(int i = 0; i < c ;i++){cout << ans[i].name ;printf(" %02d\n",ans[i].month);for(int j = 0 ; j < ans[i].list.size();j++){printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.02lf\n",ans[i].list[j].n1.dd,ans[i].list[j].n1.hh,ans[i].list[j].n1.mm,ans[i].list[j].n2.dd,ans[i].list[j].n2.hh,ans[i].list[j].n2.mm,ans[i].list[j].SumMin,ans[i].list[j].sum);}printf("Total amount: $%.02lf\n",ans[i].sum);}return 0;
}
……………………………………完……………………………………

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>using namespace std;struct Node {string name;string time;string state;
};int char2int(char i) {int result = 0;switch (i){case '1': result = 1; break;case '2': result = 2; break;case '3': result = 3; break;case '4': result = 4; break;case '5': result = 5; break;case '6': result = 6; break;case '7': result = 7; break;case '8': result = 8; break;case '9': result = 9; break;case '0': result = 0; break;default:break;}return result;
}int str2day(string s) {int day = 0;day = char2int(s[3]) * 10 + char2int(s[4]);return day;
}int str2hour(string s) {int hour = 0;hour = char2int(s[6]) * 10 + char2int(s[7]);return hour;
}int str2minute(string s) {int minute = 0;minute = char2int(s[9]) * 10 + char2int(s[10]);return minute;
}int CalRate(string time1, string time2, vector <int> rate) {//M1 < M2int D1, D2, H1, H2, M1, M2;D1 = str2day(time1);D2 = str2day(time2);H1 = str2hour(time1);H2 = str2hour(time2);M1 = str2minute(time1);M2 = str2minute(time2);int Sum = 0;if (D1 == D2) { //同一天if (H1 == H2) {//同一小时Sum += (M2 - M1)* rate[H1];return Sum;}else{//不同小时Sum += (60 - M1) * rate[H1];Sum += M2 * rate[H2];for (int i = H1+1; i < H2; i++)Sum += 60 * rate[i];return Sum;}}else{ //不同天Sum += (60 - M1)* rate[H1];for (int i = H1 + 1; i < 24; i++) {Sum += 60 * rate[i];}Sum += M2 * rate[H2];for (int i = 0; i < H2; i++) {Sum += 60 * rate[i];}for (int i = D1 + 1; i < D2; i++) {for (int j = 0; j < 24; j++) {Sum += rate[j] * 60;}}return Sum;}}void PrintTime(string s) {for (int i = 3; i <= 10; i++) {cout << s[i];}
}void TotalRate(vector <Node> list ,vector <int> rate) { //对一个用户的账单进行生成bool tag = false;//是否匹配bool Head = true;//又没有显示Headbool HaveBill = false;float SumRate = 0;int time = 0;Node pre; //匹配前//cout << list[0].name << " " << list[0].time[0] << list[0].time[1] << endl;for (int i = 0; i < list.size(); i++) {if (!tag && list[i].state == "off-line")//前面没有on 却有off 忽略continue;else if (!tag && list[i].state == "on-line") {//前面没有on 后面weion pre等于该数tag = true;pre = list[i];}else if (tag && list[i].state == "on-line") { //前面有on 后面还有on 更新prepre = list[i];}else {//匹配成功if(Head){cout << list[i].name << " " << list[i].time[0] << list[i].time[1] << endl;Head = false;HaveBill = true;}tag = false;int PreTime = str2day(pre.time) * 24 * 60 + str2hour(pre.time) * 60 + str2minute(pre.time);int Time = str2day(list[i].time) * 24 * 60 + str2hour(list[i].time) * 60 + str2minute(list[i].time);int gapTime = Time - PreTime;float tempRate = CalRate(pre.time, list[i].time, rate);float tR = tempRate / 100;SumRate += tR;PrintTime(pre.time); cout << " ";PrintTime(list[i].time);cout << " " << gapTime << " $";printf("%.02f", tR);cout << endl;}}if (HaveBill) {cout << "Total amount: $";printf("%.2f", SumRate);cout << endl;}
}bool cmp(Node N1 ,Node N2) {if (N1.name < N2.name)return true;else if (N1.name == N2.name && str2day(N1.time) < str2day(N2.time))return true;else if (N1.name == N2.name && str2day(N1.time) == str2day(N2.time) && str2hour(N1.time) < str2hour(N2.time))return true;else if (N1.name == N2.name && str2day(N1.time) == str2day(N2.time) && str2hour(N1.time) == str2hour(N2.time) && str2minute(N1.time) < str2minute(N2.time))return true;elsereturn false;
}int main() {vector <int> rate;	int tempRate;for (int i = 0; i < 24; i++) {cin >> tempRate;rate.push_back(tempRate);}int N;//记录数cin >> N;Node * record = new Node[N];set<string> r;for (int i = 0; i < N; i++) {cin >> record[i].name >> record[i].time >> record[i].state;r.insert(record[i].name);}sort(record, record + N, cmp);//对记录进行排序vector <Node> * List = new vector<Node>[r.size()]; //对不同用户进行分组string temp = record[0].name;int tempi = 0;for (int i = 0; i < N; i++) {//分组if (temp == record[i].name)List[tempi].push_back(record[i]);else{tempi++;temp = record[i].name;List[tempi].push_back(record[i]);}}//for (int i = 0; i < r.size(); i++) {//	for (int j = 0; j < List[i].size(); j++)//		cout << List[i][j].name << " " << List[i][j].time << " " << List[i][j].state << endl;//}for (int i = 0; i < r.size(); i++) {TotalRate(List[i], rate);}return 0;}


这篇关于1016. Phone Bills 解析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/426016

相关文章

C语言中自动与强制转换全解析

《C语言中自动与强制转换全解析》在编写C程序时,类型转换是确保数据正确性和一致性的关键环节,无论是隐式转换还是显式转换,都各有特点和应用场景,本文将详细探讨C语言中的类型转换机制,帮助您更好地理解并在... 目录类型转换的重要性自动类型转换(隐式转换)强制类型转换(显式转换)常见错误与注意事项总结与建议类型

MySQL 缓存机制与架构解析(最新推荐)

《MySQL缓存机制与架构解析(最新推荐)》本文详细介绍了MySQL的缓存机制和整体架构,包括一级缓存(InnoDBBufferPool)和二级缓存(QueryCache),文章还探讨了SQL... 目录一、mysql缓存机制概述二、MySQL整体架构三、SQL查询执行全流程四、MySQL 8.0为何移除查

在Rust中要用Struct和Enum组织数据的原因解析

《在Rust中要用Struct和Enum组织数据的原因解析》在Rust中,Struct和Enum是组织数据的核心工具,Struct用于将相关字段封装为单一实体,便于管理和扩展,Enum用于明确定义所有... 目录为什么在Rust中要用Struct和Enum组织数据?一、使用struct组织数据:将相关字段绑

使用Java实现一个解析CURL脚本小工具

《使用Java实现一个解析CURL脚本小工具》文章介绍了如何使用Java实现一个解析CURL脚本的工具,该工具可以将CURL脚本中的Header解析为KVMap结构,获取URL路径、请求类型,解析UR... 目录使用示例实现原理具体实现CurlParserUtilCurlEntityICurlHandler

深入解析Spring TransactionTemplate 高级用法(示例代码)

《深入解析SpringTransactionTemplate高级用法(示例代码)》TransactionTemplate是Spring框架中一个强大的工具,它允许开发者以编程方式控制事务,通过... 目录1. TransactionTemplate 的核心概念2. 核心接口和类3. TransactionT

数据库使用之union、union all、各种join的用法区别解析

《数据库使用之union、unionall、各种join的用法区别解析》:本文主要介绍SQL中的Union和UnionAll的区别,包括去重与否以及使用时的注意事项,还详细解释了Join关键字,... 目录一、Union 和Union All1、区别:2、注意点:3、具体举例二、Join关键字的区别&php

Spring IOC控制反转的实现解析

《SpringIOC控制反转的实现解析》:本文主要介绍SpringIOC控制反转的实现,IOC是Spring的核心思想之一,它通过将对象的创建、依赖注入和生命周期管理交给容器来实现解耦,使开发者... 目录1. IOC的基本概念1.1 什么是IOC1.2 IOC与DI的关系2. IOC的设计目标3. IOC

java中的HashSet与 == 和 equals的区别示例解析

《java中的HashSet与==和equals的区别示例解析》HashSet是Java中基于哈希表实现的集合类,特点包括:元素唯一、无序和可包含null,本文给大家介绍java中的HashSe... 目录什么是HashSetHashSet 的主要特点是HashSet 的常用方法hasSet存储为啥是无序的

Linux中shell解析脚本的通配符、元字符、转义符说明

《Linux中shell解析脚本的通配符、元字符、转义符说明》:本文主要介绍shell通配符、元字符、转义符以及shell解析脚本的过程,通配符用于路径扩展,元字符用于多命令分割,转义符用于将特殊... 目录一、linux shell通配符(wildcard)二、shell元字符(特殊字符 Meta)三、s

使用Python实现批量访问URL并解析XML响应功能

《使用Python实现批量访问URL并解析XML响应功能》在现代Web开发和数据抓取中,批量访问URL并解析响应内容是一个常见的需求,本文将详细介绍如何使用Python实现批量访问URL并解析XML响... 目录引言1. 背景与需求2. 工具方法实现2.1 单URL访问与解析代码实现代码说明2.2 示例调用