分析
记住这些技巧就好:
- 闰年判断的函数写法
- 日期的处理方法(数组+循环)
数组:a[x][0]
是平年第x月的月份天数,a[x][1]
是闰年第x月的月份天数。
注意:数组的开头a[0]={0,0};
方便处理,毕竟第一个月是1月,不是0月。
代码
#include<bits/stdc++.h>
using namespace std;
int is_run(int year){//判断是不是闰年
if(year%400==0||(year%4==0 and year%100!=0))
return 1;
return 0;
}
int a[13][2]={{0,0},{31,31},{28,29},{31,31},{30,30},
{31,31},{30,30},{31,31},{31,31},{30,30},{31,31},{30,30},{31,31}};
//记住是{}
int main(){
int y,d;
cin>>y>>d;
int x=is_run(y);
int m=1;
while(1){
if(d-a[m][x]>0){
d-=a[m][x];
m++;
}else{
cout<<m<<endl<<d<<endl;
return 0;
}
}
}