清华大学机试-今年的第几天

在这里插入图片描述
在这里插入图片描述
经典问题。
值得一提的是,我本来使用的是count变量,但却提示ambiguous,不知道为什么
最后使用了cnt计数。

#include<bits/stdc++.h>
using namespace std;
int dayOfMonth[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};
const int maxn = 5000;
bool isYeap(int y){
	if(y%100==0){
		if(y%400==0)  return true;
		else return false;
	}
	else{
		if(y%4==0)  return true;
		else  return false;
	}
}
struct Time{
	int y;
	int m;
	int d;
	void nextDay(){
		d++;
		if(d>dayOfMonth[m][isYeap(y)]){
			d = 1;
			m++;
			if(m>12){
				m = 1;
				y++;
			}
		}
	}
};
int buf[maxn][13][32];
int cnt;
int main(){
	Time t;
	t.d = 1;
	t.m = 1;
	t.y = 1;
	cnt = 0; 
	while(t.y<=maxn){
		buf[t.y][t.m][t.d] = cnt;
		t.nextDay();
		cnt++;
	}
	int y,m,d;
	while(scanf("%d %d %d",&y,&m,&d)!=EOF){
		printf("%d\n",buf[y][m][d]-buf[y][1][1]+1);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_37762592/article/details/88757837