【PAT Advanced Level】1016. Phone Bills (25)

注意細心,簡單模擬題。

#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <memory.h>
using namespace std;

const int r = 24;

struct aCall
{
	int month;
	int day;
	int hour;
	int minute;
	string status;
	aCall(int mo, int d, int h, int m, string s) : month(mo), day(d), hour(h), minute(m), status(s) {}
};

int rate[r];
map<string, vector<aCall>> m;

bool cmp(const aCall &a1, const aCall &a2)
{
	if(a1.month < a2.month)	return true;
	else if(a1.month > a2.month) return false;
	else if(a1.day < a2.day) return true;
	else if(a1.day > a2.day) return false;
	else if(a1.hour < a2.hour) return true;
	else if(a1.hour > a2.hour) return false;
	else if(a1.minute < a2.minute) return true;
	else return false;
}

int compute_money(const char *name, aCall a1, aCall a2)
{
	aCall tmp = a1;
	int result = 0;
	int totalTime = 0;
	while (tmp.day < a2.day || tmp.hour < a2.hour || tmp.minute < a2.minute)
	{
		result += rate[tmp.hour];
		tmp.minute++;
		totalTime++;
		if(tmp.minute == 60)
		{
			tmp.minute = 0;
			tmp.hour++;
			if(tmp.hour == 24)
			{
				tmp.hour = 0;
				tmp.day++;
			}
		}
	}
	printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n", a1.day, a1.hour, a1.minute, a2.day, a2.hour, a2.minute, totalTime, result/100.0);
	return result;
}

int main()
{
	//freopen("a.txt", "r", stdin);

	for(int i = 0; i < r; i++)
		scanf("%d", &rate[i]);
	
	int count;
	scanf("%d", &count);
	char name[30];
	char status[10];
	int month, day, hour, minute;
	for(int i = 0; i < count; i++)
	{
		scanf("%s", &name);
		scanf("%02d:%02d:%02d:%02d", &month, &day, &hour, &minute);
		scanf("%s", &status);
		aCall a(month, day, hour, minute, status);
		if(m.find(name) != m.end())
			m[name].push_back(a);
		else
		{
			vector<aCall> tmp;
			tmp.push_back(a);
			m.insert(make_pair(name, tmp));
		}
		memset(name, 0, sizeof(name)/sizeof(char));
		memset(status, 0, sizeof(status)/sizeof(char));
	}
	
	for(map<string, vector<aCall>>::iterator it = m.begin(); it != m.end(); it++)
	{
		sort(it->second.begin(), it->second.end(), cmp);
		vector<aCall> tmp = it->second;
		bool flag = true;
		int total = 0;
		for(vector<aCall>::iterator vit = tmp.begin(); vit != tmp.end() && (vit + 1) != tmp.end(); vit++)
		{
			if(vit->status != "on-line") continue;
			if((vit + 1)->status != "off-line") continue;
			if(flag) 
			{
				printf("%s %02d\n", it->first.c_str(), vit->month);
				flag = false;
			}
			total += compute_money(it->first.c_str(), *vit, *(vit + 1));
			vit++;
		}
		if(!flag)
			printf("Total amount: $%.2f\n", total/100.0);

	}

}



發佈了66 篇原創文章 · 獲贊 8 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章