75-日期计算
内存限制:64MB 时间限制:3000ms 特判: No
通过数:19 提交数:31 难度:1
题目描述:
如题,输入一个日期,格式如:2010 10 24 ,判断这一天是这一年中的第几天。
输入描述:
第一行输入一个数N(0<N<=100),表示有N组测试数据。后面的N行输入多组输入数据,每行的输入数据都是一个按题目要求格式输入的日期。
输出描述:
每组输入数据的输出占一行,输出判断出的天数n
样例输入:
3 2000 4 5 2001 5 4 2010 10 24
样例输出:
96 124 297
分析:
1、要注意的是闰年leap year和平年common year的判断
2、闰年是366天,条件是(n%400 == 0 || (n%4 == 0 && n%100))
3、平年是365天,出去闰年的就是平年
C/C++代码实现(AC):
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <cstdio> 5 #include <cmath> 6 #include <stack> 7 #include <map> 8 #include <queue> 9 #include <set> 10 11 using namespace std; 12 const int MAXN = 15; 13 const int leap_year[MAXN] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 14 const int common_year[MAXN] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 15 16 bool is_leap(int n) 17 { 18 if ((n % 400) == 0) return true; 19 if ((n % 4) == 0 && (n % 100)) return true; 20 return false; 21 } 22 23 int main() 24 { 25 int t; 26 scanf("%d", &t); 27 while(t --) 28 { 29 int y, m, d, cnt = 0; 30 scanf("%d%d%d", &y, &m, &d); 31 if(is_leap(y)) 32 { 33 for (int i = 1; i < m; ++ i) 34 cnt += leap_year[i]; 35 cnt += d; 36 } 37 else 38 { 39 for (int i = 1; i < m; ++ i) 40 cnt += common_year[i]; 41 cnt += d; 42 } 43 printf("%d\n", cnt); 44 } 45 return 0; 46 }