- 總時間限制:
- 1000ms
- 內存限制:
- 65536kB
- 描述
- 小王是一家公司的人力資源部門的經理,現在她想知道誰在公司呆的天數最長。每天員工都有一個進入
公司的日期,和離開公司的日期。如果員工還在公司工作,那麼他的離開日期就是當前的日期。編寫一
個程序,計算每個員工在公司的天數,並按照天數從大到小排序,如果兩個員工的天數相同,則按輸入的先後次序排序。
- 輸入
-
只有一個測試樣例。第一行有一個整數n,表示這組測試數據共有n行。其後n行,每行的每一個是字符串,表示人名,其長度不超過10。後6個是整數,各個值之間
用一個空格隔開。第一個數表示員工加入公司的年份,第二個數表示員工加入公司的月份,第三個數表示員工加入公司的日期。第四個數表示員工離開公司的年份,第五個數表示員工離開公司的月份,每六個數表示員工離開公司的日期。輸入的日期不小於1900 1 1 不大於9999 12 31
在我們現在使用的日曆中, 閏年被定義爲能被4整除的年份,但是能被100整除而不能被400整除的年是例外,它們不是閏年。例如:1700, 1800, 1900 和 2100 不是閏年,而 1600, 2000 和 2400是閏年。 - 輸出
- 計算每個員工在公司所呆的天數,並按所呆的天數長短進行排序,如果兩個員工的天數相同,則按輸入順序排序。
- 樣例輸入
-
3 john 2007 10 1 2007 10 2 abbot 2008 2 21 2008 3 1 alcott 2006 2 20 2006 3 1
- 樣例輸出
-
abbot 10 alcott 10 john 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,否則返回0 if (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]; else days += month_1[i]; } for (int i = year_in + 1; i <= year_out; ++i){ if (is_leap_year(i)) days += 366; else days += 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]; else days -= 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; }