题目链接:3391. 今年的第几天? - AcWing题库
思路:
用前缀和预处理一下日期,对其进行一个优化,而不是单纯的暴力模拟
代码:
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
int r[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int n[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int sr[13], sn[13];
int main() {
ios::sync_with_stdio(false);
cout.tie(0);
for(int i = 1; i <= 12; i ++) {
sr[i] = sr[i - 1] + r[i];
sn[i] = sn[i - 1] + n[i];
}
int y, m, d;
while(cin >> y >> m >> d) {
if((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)) cout << sr[m - 1] + d << endl;
else cout << sn[m - 1] + d << endl;
}
return 0;
}