码迷,mamicode.com
首页 > 其他好文 > 详细

碰到日期题就怕的我来写一道水题吧

时间:2016-12-19 08:38:57      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:style   二维数组   span   problem   哈哈   lan   日期   pid   class   

HDOJ-2005,

http://acm.hdu.edu.cn/showproblem.php?pid=2005

20XX系列的水题哈哈,写了二十分钟,就为找到一种比较正常不傻逼的写法。。。

嗯,学习了一下,闰年的判断可以写成一个接受参数的宏。

#define lev(n) (n%4==0&&(n%100!=0||n%400==0))

然后建立一个二维数组来存储闰年和非闰年的每月天数。

int calendar[2][13] = {
    {0,31,28,31,30,31,30,31,31,30,31,30,31},
    {0,31,29,31,30,31,30,31,31,30,31,30,31 }
}

然后 calendar[luv(year)][i] 就是i月的天数啦!

附2005AC代码:

 1 #include <stdio.h>
 2 #include <math.h>
 3 #define lev(n) (n%4==0&&(n%100!=0||n%400==0))
 4 
 5 int main() {
 6     int calendar[2][13] = {
 7         {0,31,28,31,30,31,30,31,31,30,31,30,31},
 8         {0,31,29,31,30,31,30,31,31,30,31,30,31 }
 9     };
10     int year, month, day;
11     int i, count;
12     while (~scanf("%d/%d/%d", &year, &month, &day)) {
13         for (i = 1, count = 0; i < month; i++) {
14             count += calendar[lev(year)][i];
15         }
16         printf("%d\n", count+day);
17     }
18 }

 

碰到日期题就怕的我来写一道水题吧

标签:style   二维数组   span   problem   哈哈   lan   日期   pid   class   

原文地址:http://www.cnblogs.com/ray-coding-in-rays/p/6196122.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!