当前位置:Gxlcms > html代码 > 【TopCoderSRM157DIV1】Salary解题报告_html/css_WEB-ITnose

【TopCoderSRM157DIV1】Salary解题报告_html/css_WEB-ITnose

时间:2021-07-01 10:21:17 帮助过:24人阅读

本来想把原题给贴过来的,后来看到人家有版权保护,在网上搜了一下,好像也没有人在博客中贴过原题,为避免麻烦,就不copy人家原题了。

【题目大意】

公司按照每小时 wage 元给员工发工资,员工上班打卡记录一个时间,下班记录一个时间,时间格式为 "hh:mm:ss"。其中,18:00:00 到 23:59:59 和 00:00:00 到 05:59:59 之间算加班时间,工资为1.5倍。计算工资时,分钟和秒都换算成小时来计算,最终的工资取整数,舍弃后面的小数。

【函数格式】

int howMuch(vector  arrival, vector  departure, int wage)

【注意】

arrival和departure元素个数一样,且arrival[0]【解题思路】

思路不难,只是时间区间判断比较麻烦,有两个时间关键点06:00:00和18:00:00。

【我的代码】

class Salary {public:	int compareTime(int hh1, int mm1, int ss1, int hh2, int mm2, int ss2) {		if (hh1 > hh2 || (hh1 == hh2 && mm1 > mm2) || (hh1 == hh2 && mm1 == mm2 && ss1 > ss2)) {			return 1;		}		if (hh1 < hh2 || (hh1 == hh2 && mm1 < mm2) || (hh1 == hh2 && mm1 == mm2 && ss1 < ss2)) {			return -1;		}		return 0;	}	double duringTime(int hh1, int mm1, int ss1, int hh2, int mm2, int ss2) {		double time = 0;		if (ss2 >= ss1) {			time += (ss2 - ss1) / 3600.0;		} else {			time += (ss2 + 60 - ss1) / 3600.0;			mm2 -= 1;		}		if (mm2 >= mm1) {			time += (mm2 - mm1) / 60.0;		} else {			time += (mm2 + 60 - mm1) / 60.0;			hh2 -= 1;		}		time += hh2 - hh1;		return time;	}	int howMuch(vector  arrival, vector  departure, int wage) {		int beginHour, endHour, beginMinute, endMinute, beginSecond, endSecond;		stringstream ss;		double total = 0;		for (int i = 0; i < arrival.size(); i++) {			string time1 = arrival[i];			ss.clear();			ss << time1.substr(0, 2);			ss >> beginHour;			ss.clear();			ss << time1.substr(3, 2);			ss >> beginMinute;			ss.clear();			ss << time1.substr(6, 2);			ss >> beginSecond;			string time2 = departure[i];			ss.clear();			ss << time2.substr(0, 2);			ss >> endHour;			ss.clear();			ss << time2.substr(3, 2);			ss >> endMinute;			ss.clear();			ss << time2.substr(6, 2);			ss >> endSecond;			if (compareTime(beginHour, beginMinute, beginSecond, 6, 0, 0) < 0) {				if (compareTime(endHour, endMinute, endSecond, 6, 0, 0) < 0) {					total += duringTime(beginHour, beginMinute, beginSecond, endHour, endMinute, endSecond) * wage * 1.5;				} else if (compareTime(endHour, endMinute, endSecond, 6, 0, 0) >= 0						&& compareTime(endHour, endMinute, endSecond, 18, 0, 0) < 0) {					total += duringTime(beginHour, beginMinute, beginSecond, 6, 0, 0) * wage * 1.5;					total += duringTime(6, 0, 0, endHour, endMinute, endSecond) * wage;				} else if (compareTime(endHour, endMinute, endSecond, 18, 0, 0) >= 0) {					total += duringTime(beginHour, beginMinute, beginSecond, 6, 0, 0) * wage * 1.5;					total += duringTime(6, 0, 0, 18, 0, 0) * wage;					total += duringTime(18, 0, 0, endHour, endMinute, endSecond) * wage * 1.5;				}			} else if (compareTime(beginHour, beginMinute, beginSecond, 18, 0, 0) < 0) {				if (compareTime(endHour, endMinute, endSecond, 18, 0, 0) < 0) {					total += duringTime(beginHour, beginMinute, beginSecond, endHour, endMinute, endSecond) * wage;				} else if (compareTime(endHour, endMinute, endSecond, 18, 0, 0) >= 0) {					total += duringTime(beginHour, beginMinute, beginSecond, 18, 0, 0) * wage;					total += duringTime(18, 0, 0, endHour, endMinute, endSecond) * wage * 1.5;				}			} else {				total += duringTime(beginHour, beginMinute, beginSecond, endHour, endMinute, endSecond) * wage * 1.5;			}		}		return (int)total;	}};

首先是字符串转换成时分秒整数值。两个自定义函数功能分别为:比较两个时间点先后,主要是和两个关键时间点06:00:00和18:00:00比较;计算两个时间点之间的小时数。

然后一大段逻辑判断,判断员工工作时间是不是在00:00:00和06:00:00之间、06:00:00和18:00:00之间、18:00:00和00:00:00之间,还是说跨时间段。

由于基础不好,代码写得比较?嗦,欢迎大家改进。

人气教程排行