第几天

来源:互联网 发布:金庸群侠传x mac 编辑:程序博客网 时间:2024/04/28 12:59

  • [1007] 第几天

  • 时间限制: 1000 ms 内存限制: 65535 K
  • 问题描述
  • 有一本记录了从1年到9999年的日历,

    假设1年1月1日为第一天,现在问第Y年的第M月的第D天是第几天。


  • 输入
  • 输入n,代表有n组数据。接下来n行数据每行输入三个整数代表要询问的年月日。
  • 输出
  • 对于每组数据,输出这是第几天。
  • 样例输入
  • 21 1 12 2 2
  • 样例输出
  • 1398
  • 提示
  • 来源
  • HJX
  • 操作
  •     


注意闰年的判断:“四年一闰,百年不闰,四百年再闰“,记得加括号保证优先级。

#include"cstdio"#include"cstring"#include"iostream"#include"algorithm"using namespace std;bool is_leap(int year){     if((year%4 == 0 && year%100 != 0) || (year%400 == 0))    {        return true;    }    return false;}int lmon[13] = {0,31,29,31,30,31,30,31,31,30,31,30,31};int mon[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};int main(){    int n;    while(scanf("%d",&n) != EOF)    {        while(n--)        {            int year,month,day;            scanf("%d%d%d",&year,&month,&day);            int ans = 0;            for(int i = 1;i < year;i++)            {                if(is_leap(i))                {                    ans += 366;                }                else                {                    ans += 365;                }            }            for(int i = 1;i < month;i++)            {                if(is_leap(year))                {                    ans += lmon[i];                }                else                {                    ans += mon[i];                }            }            ans += day;            printf("%d\n",ans);        }    }    return 0;}


0 0
原创粉丝点击