【题目大意】
公司按照每小时 wage 元给员工发工资,员工上班打卡记录一个时间,下班记录一个时间,时间格式为 "hh:mm:ss"。其中,18:00:00 到 23:59:59 和 0 到 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 = 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 > beginHour; ss.clear(); ss > beginMinute; ss.clear(); ss > beginSecond; string time2 = departure[i]; ss.clear(); ss > endHour; ss.clear(); ss > endMinute; ss.clear(); ss > endSecond; if (compareTime(beginHour, beginMinute, beginSecond, 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, 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) { 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比较;计算两个时间点之间的小时数。
然后一大段逻辑判断,判断员工工作时间是不是在0和06:00:00之间、06:00:00和18:00:00之间、18:00:00和0之间,还是说跨时间段。
由于基础不好,代码写得比较?嗦,欢迎大家改进。查看更多关于【TopCoderSRM157DIV1】Salary解题报告_html/css_WEB-ITnose的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did105515