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

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 }

 

转载于:https://www.cnblogs.com/ray-coding-in-rays/p/6196122.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值