CCF-CSP 201509-2 Date calculation

Problem description
  Given a year y and an integer d, ask what day is the day d of the year?
  Note that there are 29 days in February in leap years. A leap
  year is met if one of the following conditions is met: 1) The year is an integer multiple of 4, and not an integer multiple of 100;
  2) The year is an integer multiple of 400.
Input format
  The first line of input contains an integer y, indicating the year, which is between 1900 and 2015 (including 1900 and 2015).
  The second line of input contains an integer d, d between 1 and 365.
Output format
  outputs two lines, one integer for each line, indicating the month and date of the answer.
Sample input
2015
80
sample output
3
21
sample input
2000
40
sample output
2
9

Summary of experience:
Create a two-dimensional array to store the number of days in each month of leap years and years.
Accumulate from the first month, and judge whether to accumulate to the corresponding month.
Note: Note that there is a case of equality.
Note: You can also create a one-dimensional array and modify the number of days in the second month when determining whether it is a leap year.

C ++ code:

#include<bits/stdc++.h>
using namespace std;
int years[2][12] = {31,28,31,30,31,30,31,31,30,31,30,31,
					31,29,31,30,31,30,31,31,30,31,30,31};
int main() {
	int year,day,flag = 0;
	scanf("%d %d",&year,&day);
	if(0 == year%4 && 0 != year%100 || 0 == year%400){
		flag = 1;
	}
	int sum = 0;
	for(int i=0;i<12;i++){
		if(sum + years[flag][i]>=day){ //注意还有等于的情况
			printf("%d\n%d\n",i+1,day-sum);
			break;
		}
		sum += years[flag][i];
	}
	return 0;
}
Published 111 original articles · won praise 2 · Views 3533

Guess you like

Origin blog.csdn.net/m0_38088647/article/details/100639032