CCF-CSP201509-2 日期计算

题目链接

问题描述

试题编号: 201509-2
试题名称: 日期计算
时间限制: 1.0s
内存限制: 256.0MB
问题描述:

问题描述

  给定一个年份y和一个整数d,问这一年的第d天是几月几日?
  注意闰年的2月有29天。满足下面条件之一的是闰年:
  1) 年份是4的整数倍,而且不是100的整数倍;
  2) 年份是400的整数倍。

输入格式

  输入的第一行包含一个整数y,表示年份,年份在1900到2015之间(包含1900和2015)。
  输入的第二行包含一个整数dd在1至365之间。

输出格式

  输出两行,每行一个整数,分别表示答案的月份和日期。

样例输入

2015
80

样例输出

3
21

样例输入

2000
40

样例输出

2
9

大月:1、3、5、7、8、10、12

用数组保存到某月的天数,利用lower_bound函数求解。

AC代码:

 1 #include<iostream>
 2 #include<sstream>
 3 #include<algorithm>
 4 #include<string>
 5 #include<cstring>
 6 #include<iomanip>
 7 #include<vector>
 8 #include<cmath>
 9 #include<ctime>
10 #include<stack>
11 #include<queue>
12 #define e 2.71828182
13 #define Pi 3.141592654
14 using namespace std;
15 int isleap(int year)
16 {
17     if((year%4==0&&year%100!=0)||(year%400==0)) return 1;
18     else return 0;
19 }
20 int main()
21 {
22     int y,d;
23     int month[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}};
24     for(int i=0;i<2;i++)
25         for(int j=1;j<=12;j++)
26             month[i][j]+=month[i][j-1];
27     
28     cin>>y>>d;
29     int wz=isleap(y);//闰年为1,平年为0 
30     int m=lower_bound(month[wz],month[wz]+13,d)-month[wz];//月份 
31     cout<<m<<endl<<d-month[wz][m-1];
32 }
View Code
原文地址:https://www.cnblogs.com/wangzhebufangqi/p/12796124.html