UVa 12439 - February 29

来源:互联网 发布:mysql emoji 编辑:程序博客网 时间:2024/05/22 10:38

題目:給你兩個日期,判斷中間有多少個2月29日。

分析:數論。閏年判斷規則,四年一閏,百年不閏,四百年閏。

            直接統計從0年0月1日到當前時間中有多少閏年,做差即可;

            注意本年的2月29是否在區間中即可。

說明:注意判斷時間的先後╮(╯▽╰)╭。

#include <cstring>#include <cstdio>int leapCount(char month[], int day, int year){int ans = year/400*97 + year%400/100*24 + year%100/4;if (year%400 == 0 || year%100 && year%4 == 0)if (!strcmp("January", month))ans --;else if(!strcmp("February", month) && day < 29)ans --;return ans;}int main(){int  n, day, year, cases = 1;char month[10];while (~scanf("%d",&n)) {scanf("%s %d, %d",month,&day,&year);int ans = leapCount(month, day-1, year);scanf("%s %d, %d",month,&day,&year);ans -= leapCount(month, day, year);if (ans < 0) ans = -ans;printf("Case %d: %d\n",cases ++, ans);}    return 0;}


0 0
原创粉丝点击