POJ 3715:计算工作天数

来源:互联网 发布:淘宝锋利小刀 编辑:程序博客网 时间:2024/05/01 21:21
总时间限制: 
1000ms 
内存限制: 
65536kB
描述
小王是一家公司的人力资源部门的经理,现在她想知道谁在公司呆的天数最长。每天员工都有一个进入
公司的日期,和离开公司的日期。如果员工还在公司工作,那么他的离开日期就是当前的日期。编写一
个程序,计算每个员工在公司的天数,并按照天数从大到小排序,如果两个员工的天数相同,则按输入

的先后次序排序。

输入
只有一个测试样例。第一行有一个整数n,表示这组测试数据共有n行。其后n行,每行的每一个是字符串,表示人名,其长度不超过10。后6个是整数,各个值之间
用一个空格隔开。第一个数表示员工加入公司的年份,第二个数表示员工加入公司的月份,第三个数表示员工加入公司的日期。第四个数表示员工离开公司的年份,第五个数表示员工离开公司的月份,每六个数表示员工离开公司的日期。输入的日期不小于1900 1 1 不大于9999 12 31
在我们现在使用的日历中, 闰年被定义为能被4整除的年份,但是能被100整除而不能被400整除的年是例外,它们不是闰年。例如:1700, 1800, 1900 和 2100 不是闰年,而 1600, 2000 和 2400是闰年。

输出
计算每个员工在公司所呆的天数,并按所呆的天数长短进行排序,如果两个员工的天数相同,则按输入顺序排序。
样例输入
3john 2007 10 1 2007 10 2abbot 2008 2 21 2008 3 1alcott 2006 2 20 2006 3 1
样例输出
abbot 10alcott 10john 2


好久没做poj,挑一个水题来练练手,大笑,这题主要是细心啊。

这题用的是vector来储存每个人员的工作信息,使用模板算法stable_sort来排序。

计算年的方法是:例如,从1900年1月1日到9999年12月1日的时间,首先计算1900年1月1日到9999年12月31日的天数,然后再减去30天即可。就是先计算入职时间和离职时间到某个时间节点的天数,然后再将它们对减即可。

#include <iostream>#include <string>#include <vector>#include <algorithm>using namespace std;int month_1[] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; // 正常年的月份int month_2[] = {0,31,29,31,30,31,30,31,31,30,31,30,31}; // 闰年的月份int n, year_in, year_out, day_in, day_out, month_in, month_out;typedef struct employee{string name;int days_in_company;} EMPLOYEE;vector<EMPLOYEE> employee_vec;bool is_leap_year(int year){ // 闰年返回1,否则返回0if (year%4==0){if ( (year%100 == 0) && (year%400 != 0) )return false;return true;}return false;}int count_days(){int days = 0;days += (is_leap_year(year_in) ?  month_2[month_in] : month_1[month_in]) - day_in + 1; for (int i = month_in + 1; i < 13; ++i){if (is_leap_year(year_in))days += month_2[i];elsedays += month_1[i];}for (int i = year_in + 1; i <= year_out; ++i){ if (is_leap_year(i))days += 366;elsedays += 365;}days -= (is_leap_year(year_out) ? month_2[month_out] : month_1[month_out]) - day_out;for (int i = month_out + 1; i < 13; ++i){if (is_leap_year(year_out))days -= month_2[i];elsedays -= month_1[i];}return days;}int isShorter(const EMPLOYEE &a, const EMPLOYEE &b){return a.days_in_company > b.days_in_company;}int main(){cin >> n;while(n--){EMPLOYEE one;cin >> one.name;cin >> year_in >> month_in >> day_in >> year_out >> month_out >> day_out;one.days_in_company = count_days();employee_vec.push_back(one);}stable_sort(employee_vec.begin(), employee_vec.end(), isShorter);for (vector<EMPLOYEE>::iterator it = employee_vec.begin(); it != employee_vec.end(); ++it){cout << (*it).name << " " << (*it).days_in_company << endl;}return 0;}




0 0
原创粉丝点击