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

相关文章

Golang HashMap实现原理解析

《GolangHashMap实现原理解析》HashMap是一种基于哈希表实现的键值对存储结构,它通过哈希函数将键映射到数组的索引位置,支持高效的插入、查找和删除操作,:本文主要介绍GolangH... 目录HashMap是一种基于哈希表实现的键值对存储结构,它通过哈希函数将键映射到数组的索引位置,支持

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http

Python利用ElementTree实现快速解析XML文件

《Python利用ElementTree实现快速解析XML文件》ElementTree是Python标准库的一部分,而且是Python标准库中用于解析和操作XML数据的模块,下面小编就来和大家详细讲讲... 目录一、XML文件解析到底有多重要二、ElementTree快速入门1. 加载XML的两种方式2.

Java的栈与队列实现代码解析

《Java的栈与队列实现代码解析》栈是常见的线性数据结构,栈的特点是以先进后出的形式,后进先出,先进后出,分为栈底和栈顶,栈应用于内存的分配,表达式求值,存储临时的数据和方法的调用等,本文给大家介绍J... 目录栈的概念(Stack)栈的实现代码队列(Queue)模拟实现队列(双链表实现)循环队列(循环数组

java解析jwt中的payload的用法

《java解析jwt中的payload的用法》:本文主要介绍java解析jwt中的payload的用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解析jwt中的payload1. 使用 jjwt 库步骤 1:添加依赖步骤 2:解析 JWT2. 使用 N

Python中__init__方法使用的深度解析

《Python中__init__方法使用的深度解析》在Python的面向对象编程(OOP)体系中,__init__方法如同建造房屋时的奠基仪式——它定义了对象诞生时的初始状态,下面我们就来深入了解下_... 目录一、__init__的基因图谱二、初始化过程的魔法时刻继承链中的初始化顺序self参数的奥秘默认

Java 正则表达式URL 匹配与源码全解析

《Java正则表达式URL匹配与源码全解析》在Web应用开发中,我们经常需要对URL进行格式验证,今天我们结合Java的Pattern和Matcher类,深入理解正则表达式在实际应用中... 目录1.正则表达式分解:2. 添加域名匹配 (2)3. 添加路径和查询参数匹配 (3) 4. 最终优化版本5.设计思

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

Java字符串处理全解析(String、StringBuilder与StringBuffer)

《Java字符串处理全解析(String、StringBuilder与StringBuffer)》:本文主要介绍Java字符串处理全解析(String、StringBuilder与StringBu... 目录Java字符串处理全解析:String、StringBuilder与StringBuffer一、St

Spring Boot循环依赖原理、解决方案与最佳实践(全解析)

《SpringBoot循环依赖原理、解决方案与最佳实践(全解析)》循环依赖指两个或多个Bean相互直接或间接引用,形成闭环依赖关系,:本文主要介绍SpringBoot循环依赖原理、解决方案与最... 目录一、循环依赖的本质与危害1.1 什么是循环依赖?1.2 核心危害二、Spring的三级缓存机制2.1 三