pat甲級 1016 Phone Bills (25 分) (字符串處理,模擬)

題目鏈接:傳送門

思路:我的思路是把一天內所有時間點的花費記錄在數組中,如果通話起止時間是在同一天,直接減就可以了,如果不是同一天有兩種情況,只相隔一天的把第一天和第二天的費用相加即可,相隔兩天以上需要算中間完整的24小時的費用。
看了大佬題解發現做麻煩了,不過代碼還是貼在這裏。。

代碼:

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1005;

struct node {
	string na , time;
	int tag;
	node(string b ,string c , int d) : na(b) , time(c) , tag(d){}
	bool operator < (const node b) const {
		return na < b.na || (na == b.na && time < b.time);
	}
};

int f1[3000];
//map <string , int> mp;
int Time;
double Cash;
vector <node> name;
vector <node> name1;

void calc(string start , string end) {
	int dd1[5] = {0} , dd2[5] = {0} , t = 0;
	for(int i = 0 ; i < start.length() ; i++) {
		if(start[i] == ':') {
			t++;
		}
		else {
			dd1[t] = dd1[t] * 10 + start[i] - '0'; 		
		}
	}
	t = 0;
	for(int i = 0 ; i < end.length() ; i++) {
		if(end[i] == ':') {
			t++;
		}
		else {
			dd2[t] = dd2[t] * 10 + end[i] - '0';
		}
	}
	double time , cash;
	if(dd1[1] == dd2[1]) {
		 time = (dd2[2] * 60 + dd2[3]) - (dd1[2] * 60 + dd1[3]);
		 cash = f1[dd2[2] * 60 + dd2[3]] - f1[dd1[2] * 60 + dd1[3]]; 
	}
	else {
		time = 0 , cash = 0;
		if(dd2[1] - dd1[1] >= 2) {
			int tmp = dd2[1] - dd1[1] - 1;
			time += 24 * 60 * tmp;
			cash += f1[24 * 60] * tmp;
		}
		time += 24 * 60 - dd1[2] * 60 - dd1[3] + dd2[2] * 60 + dd2[3];
		cash += f1[24 * 60] - f1[dd1[2] * 60 + dd1[3]] + f1[dd2[2] * 60 + dd2[3]];
	}
	Time = time;
	Cash = cash;
}


int main() {
	ios::sync_with_stdio(0);
	int tmp = 0;
	for(int i = 0 ; i < 24 ; i++) {
		cin >> tmp;
		for(int j = 1 ; j <= 60 ; j++) {
			f1[i * 60 + j] = f1[i * 60 + j - 1] + tmp;
		}
	}
	int n;
	cin >> n;
	string mon;
	string t , a , b; 
	for(int i = 0 ; i < n ; i++) {
		cin >> t >> a >> b;
		int tag;
		if(mon == "")mon = a.substr(0 , 2);
		if(b == "on-line")tag = 0;
		else tag = 1;
		name.push_back(node(t , a ,  tag));
		
 	}
 	sort(name.begin() , name.end());
 	for(int i = 0 ; i < name.size() ; i++) {	
		if(i + 1 >= name.size())continue;
		if(name[i].na == name[i + 1].na &&  name[i].tag == 0 && name[i + 1].tag == 1) {
			name1.push_back(name[i]);
			name1.push_back(name[i + 1]);
			//cout << name[i + 1].na << " " << name[i].na << "\n";
			i++;	
		} 
	}
	double ans = 0;
	string flag = name[0].na;
	for(int i = 0 ; i < name1.size() ; i += 2) {
		if(i == 0 || flag != name[i].na) {
 			if(i > 0)cout << "Total amount:" << " $" << fixed << setprecision(2) << ans << "\n"; 
			cout << name1[i].na << " " << name1[i].time.substr(0 , 2) << "\n";
			ans = 0;
		}		
		flag = name1[i].na;
		calc(name1[i].time , name1[i + 1].time);
		cout << name1[i].time.substr(3) << " " << name1[i + 1].time.substr(3) << " " << Time << " " << "$" << fixed << setprecision(2) << Cash / 100.0 << "\n";
		ans += Cash / 100.0;		
	}
	if(ans)cout << "Total amount:" << " $" << fixed << setprecision(2) << ans << "\n"; 
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章