A
#include <iostream>
using namespace std;
int cal(int x){
int res = 0;
while(x) res += (x % 10 == 2), x /= 10;
return res;
}
int main(){
int res = 0;
for(int i = 1;i <= 2020;i ++ )
res += cal(i);
printf("%d\n", res);
return 0;
}
B
#include <iostream>
using namespace std;
const int N = 2021;
int gcd(int a, int b){
return b == 0 ? a : gcd(b, a % b);
}
int main(){
int res = 0;
for(int i = 1;i < N;i ++ )
for(int j = 1;j < N;j ++ )
if(gcd(i, j) == 1) res ++ ;
printf("%d\n", res);
return 0;
}
C
找规律
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int delta = 4;
int start = 1;
for(int i = 1;i < 20;i ++ ){
start += delta;
delta += 4;
}
cout << start << endl;
return 0;
}
暴力模拟
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 1000;
int g[N][N];
bool check(int x){
if(x <= 0) return false;
return true;
}
int main(){
int x = 1, y = 1;
int dir = -1, now = 1;
for(int i = 1;i <= 10000;){
g[x][y] = i ++ ;
// cout << g[x][y] << endl;
int nx = x + dir, ny = y - dir;
if(!check(nx) || !check(ny)){
dir *= -1;
if(!check(nx)) nx = 1, ny = ++ now;
else ny = 1, nx = ++ now;
}
x = nx, y = ny;
}
for(int i = 1;i <= 20;i ++ ){
for(int j = 1;j <= 20;j ++ )
cout << g[i][j] << ' ';
cout << endl;
}
return 0;
}
D
暴力模拟就行
E
繁琐建图,然后二进制枚举,最后 dfs 或者 bfs check 是否合法就行
F
#include <iostream>
#include <cmath>
using namespace std;
int good, pass;
int main(){
int n; scanf("%d", &n);
for(int i = 1;i <= n;i ++ ){
int x; scanf("%d", &x);
if(x >= 60) good ++ ;
if(x >= 85) pass ++ ;
}
printf("%.0lf\%\n", round(good * 1.0 / n * 100));
printf("%.0lf\%\n", round(pass * 1.0 / n * 100));
return 0;
}
G
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 10010, base = 10000;
int n;
string ans, res;
int dir_f[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int dir_s[23] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool check(int year){
if(year % 400 == 0 || year % 4 == 0 && year % 100 != 0) return true;
return false;
}
bool check(string now, string date){
int year = stoi(now);
int month = stoi(date.substr(0, 2));
int day = stoi(date.substr(2));
if(month > 12 || month <= 0) return false;
if(day <= 0 || day > 31) return false;
if(check(year)){
if(dir_s[month - 1] >= day) return true;
else return false;
}else{
if(dir_f[month - 1] >= day) return true;
else return false;
}
}
bool check(string now){
string year = now.substr(0, 4);
string date = now.substr(4);
if(!check(year, date)) return false;
char a = now[0], b = now[1];
if(now[2] != a || now[5] != a || now[7] != a) return false;
if(now[3] != b || now[4] != b || now[6] != b) return false;
return true;
}
int main(){
scanf("%d", &n);
for(int i = n / base + 1;;i ++ ){
string now = to_string(i);
string another = now; reverse(another.begin(), another.end());
// cout << now << endl;
if(check(now, another)){
ans = now + another;
break;
}
}
for(int i = n / base + 1;;i ++ ){
string now = to_string(i);
string another = now; reverse(another.begin(), another.end());
now += another;
// cout << now << endl;
if(check(now)){
res = now;
break;
}
}
cout << ans << endl;
cout << res << endl;
return 0;
}
H
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 100010;
// last 记录每个字母最后一个位置出现是在哪里
char s[N];
int last[26];
int f[N];
int main(){
scanf("%s", s + 1); int n = strlen(s + 1);
f[0] = 0;
for(int i = 1;i <= n;i ++ ){
f[i] += f[i - 1];
f[i] += i - last[s[i] - 'a'];
last[s[i] - 'a'] = i;
}
for(int i = 1;i <= n;i ++ ) f[i] += f[i - 1];
cout << f[n] << endl;
return 0;
}
I
每次增加交点加一个平面