#include<iostream>#include<cstring>#include<algorithm>typedeflonglong LL;usingnamespace std;int res, res2;int months[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};boolis_leap(int y,int m){if(m !=2)returnfalse;return y %4==0&& y %100!=0|| y %400==0;}boolcheck(int y,int m,int d){if(m <1|| m >12)returnfalse;if(d <1||(d > months[m]+is_leap(y, m)))returnfalse;returntrue;}intflip(int x){int res =0;while(x){
res = res *10+ x %10;
x /=10;}return res;}intmain(){int n;
cin >> n;int y = n /10000;for(int i = y; i <=9999; i ++){int yy = i, mm =0, dd =0;int t =flip(yy);
dd = t %100;
mm = t /100;
t = yy *10000+ mm *100+ dd;if(t <= n)continue;if(!res &&check(yy, mm, dd)) res = yy *10000+ mm *100+ dd;if(mm == dd && dd %10!= dd /10&&check(yy, mm, dd)){
res2 = yy *10000+ mm *100+ dd;break;}}printf("%d\n%d\n", res, res2);return0;}
子串分值和
#include<iostream>#include<cstdio>#include<cstring>#include<vector>usingnamespace std;constint N =1e5+10;char str[N];typedeflonglong LL;int pos[26];//记录每个字符最后一次出现的位置int f[N];intmain(){scanf("%s", str +1);int n =strlen(str +1);
LL ans =0;for(int i =1; i <= n;++i){int t = str[i]-'a';
f[i]= f[i -1]+ i - pos[t];
pos[t]= i;
ans += f[i];}printf("%lld\n", ans);return0;}
B组
砝码称重
#include<iostream>#include<cstring>#include<algorithm>usingnamespace std;constint N =110, M =200010, B = M /2;int n;int w[N];bool f[N][M];intmain(){scanf("%d",&n);for(int i =1; i <= n; i ++)scanf("%d",&w[i]);
f[0][B]=true;for(int i =1; i <= n; i ++)for(int j =0; j <= M ; j ++){if(f[i -1][j]){if(j - w[i]>=0) f[i][j - w[i]]=true;if(j + w[i]<= M) f[i][j + w[i]]=true;
f[i][j]=true;}}int res =0;for(int j = B +1; j <= M; j ++)
res += f[n][j];
cout << res << endl;return0;}
杨辉三角形
#include<iostream>#include<cstring>#include<algorithm>typedeflonglong LL;usingnamespace std;int n;
LL C(int a,int b){
LL res =1;for(int i = a, j =1; j <= b; i --, j ++){
res = res * i / j;if(res > n)return res;}return res;}boolcheck(int k){
LL l =2* k, r =max(LL(n), l);while(l < r){
LL mid = l + r >>1;if(C(mid, k)>= n) r = mid;else l = mid +1;}if(C(r, k)!= n)returnfalse;
cout << r *(r +1)/2+ k +1<< endl;returntrue;}intmain(){
cin >> n;for(int k =16;; k --)if(check(k))break;return0;}
C组
次数差
#include<iostream>#include<cstring>#include<algorithm>#include<unordered_map>usingnamespace std;
string s;intmain(){
unordered_map<char,int> hash;
cin >> s;for(auto& c : s)
hash[c]++;int maxn =0, minn =1010;for(auto&[c, v]: hash){
maxn =max(maxn, v);
minn =min(minn, v);}
cout << maxn - minn << endl;return0;}
等腰三角形
#include<iostream>#include<cstring>#include<algorithm>usingnamespace std;constint N =610;char g[N][N];intmain(){int n;
cin >> n;for(int i =0; i < n; i ++)for(int j =0; j < n + i; j ++)
g[i][j]='.';
string ans ="";for(int i =2; i <=500; i ++)
ans +=to_string(i);
g[0][n -1]='1';int x =1, y = n -2, k =0;int flag =1;while(x !=0|| y != n -1){
g[x][y]= ans[k ++];if(flag ==1){if(x == n -1){
y ++;
flag =2;}else{
x ++;
y --;}}elseif(flag ==2){if(y ==2* n -2){
flag =3;
x --;
y --;}else y ++;}else{
x --;
y --;}}for(int i =0; i < n; i ++)printf("%s\n", g[i]);return0;}