国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

1016. Phone Bills 解析

2019-11-14 12:38:30
字體:
供稿:網(wǎng)友

直接把時間化成以秒為單位 排序 匹配 計算就好。

#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 < M2	int 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;//又沒有顯示Head	bool 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等于該數(shù)			tag = true;			pre = list[i];		}		else if (tag && list[i].state == "on-line") { //前面有on 后面還有on 更新pre			pre = 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;	else		return false;}int main() {		vector <int> rate;		int tempRate;	for (int i = 0; i < 24; i++) {		cin >> tempRate;		rate.push_back(tempRate);	}	int N;//記錄數(shù)	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;	}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 新龙县| 宁城县| 澄江县| 会昌县| 靖州| 乌拉特中旗| 永修县| 甘德县| 六枝特区| 昭觉县| 封丘县| 乐至县| 民县| 卓资县| 镇赉县| 灌云县| 通辽市| 蓬安县| 潼关县| 三亚市| 北辰区| 榆林市| 壤塘县| 临洮县| 新河县| 罗源县| 原阳县| 黑山县| 汽车| 南陵县| 杭锦后旗| 蓝田县| 赤水市| 石门县| 台前县| 巧家县| 南雄市| 宣城市| 泗阳县| 明水县| 开鲁县|