赞
踩
无语了, 因为把判闰年 y % 400 == 0 写成了 y % 400 调了半小时
// 枚举每一天, 典型的暴力模拟 // 周一的表示方法: (sum + 5) % 7 == 1; #include <iostream> #include <cstring> #include <algorithm> using namespace std; int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int get(int y, int m) { if(m != 2) return month[m]; int leap = (y % 4 == 0 && y % 100 != 0) || y % 400 == 0; // zhuyi return leap + 28; } int main() { int sum = 5, res = 0; for(int y = 2000; y <= 2019; y ++ ) // 20200101 - 20191231; for(int m = 1; m <= 12; m ++ ) for(int d = 1; d <= get(y, m); d ++) { sum ++ ; //cout << sum % 7<<' '; if(d == 1 || sum % 7 == 1) res += 2; else res += 1; } for(int y = 2020; y <= 2020; y ++ ) // 20200101-20201001; for(int m = 1; m <= 9; m ++ ) for(int d = 1; d <= get(y, m); d ++) { sum ++ ; //cout << sum % 7<<' '; if(d == 1 || sum % 7 == 1) res += 2; else res += 1; } cout << res + 2; }
#include <bits/stdc++.h> using namespace std; int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int res; int sum = 5; //(sum + 5) % 7 == 1; int main() { for(int y = 2000; y <= 2019; y ++ ) // 年 { month[2] = 28; if(y % 100 != 0 && y % 4 == 0 || y % 400 == 0) // 特别的年 { month[2] = 29; } for(int m = 1; m <= 12; m ++) // 月 { for(int d = 1; d <= month[m]; d ++ ) // 日 { sum ++; if(d == 1 || sum % 7 == 1) res += 2; else res += 1; } } } // 2020 nian for(int m = 1; m <= 9; m ++ ) { month[2] = 29; for(int d = 1; d <= month[m]; d ++ ) { sum ++ ; if(d == 1 || sum % 7 == 1) res += 2; else res += 1; } } res += 2; cout << res; }
// 枚举每一天, 典型的暴力模拟 // 周一的表示方法: (sum + 5) % 7 == 1; #include <iostream> #include <cstring> #include <algorithm> using namespace std; int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int y, m, d; int check(int x) { y = x / 10000; m = x / 100 % 100; d = x % 100; //cout <<y << ' ' << m << ' ' << d << endl; int leap = (y % 4 == 0 && y % 100) || y % 400 == 0; if(m == 0 || m > 12 || d == 0) return 0; if(m == 2) { if(d > 28 + leap) return 0; } else { if(d > month[m]) return 0; } return 1; } int main() { int sum = 5, res = 0; for(int i = 20000101; i <= 20201001; i ++ ) { if(!check(i)) continue; //cout << i << endl; sum ++; y = i / 10000; m = i / 100 % 100; d = i % 100; if(d == 1 || sum % 7 == 1) res += 2; else res += 1; } cout << res; }
20200408
/*从 20002000 年 11 月 11 日周六(含) 到 20202020 年 1010 月 11 日周四(含)。 请问这段时间小蓝总共跑步多少千米? */ #include <bits/stdc++.h> #define deb(x) cout << #x << "=" << x << endl #define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl #define deb3(x, y, z) cout << #x << "=" << x << "," << #y << "=" << y <<","<< #z << "=" << z << endl #define deb4(x, y, z, w) cout << #x << "=" << x << "," << #y << "=" << y <<","<< #z << "=" << z << ","<< #w << "=" << w << endl #define debv(v) for(auto x:v) cout<<x<<" " #define yes cout << 'Y' << 'E' << 'S' << endl #define no cout << 'N' << 'O' << endl #define gg exit(0); using namespace std; int res = 0; // 统计千米 int sum = 5; // 统计周一 int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int main() { // 模仿 日历 [2000, 2019] for(int y = 2000; y <= 2019; y ++ ) { month[2] = 28; if((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) month[2] = 29; for(int m = 1; m <= 12; m ++ ) for(int d = 1; d <= month[m]; d ++ ) { sum ++ ; if(sum % 7 == 1 || d == 1) res += 2; // 这里写成了且 else res += 1; } } //[2020] month[2] = 29; for(int m = 1; m <= 9; m ++ ) for(int d = 1; d <= month[m]; d ++ ) { sum ++ ; //deb3( m, d, sum % 7); if(sum % 7 == 1 || d == 1) res += 2; else res += 1; } res += 2; // 没有加上最后一天 cout << res << endl; return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。