第十四届蓝桥杯校内模拟赛(第二期)
个人代码分享(不一定对,欢迎交流)
T1
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
string to_2(int x)
{
string res;
while(x)
{
res+=(x&1)+'0';
x>>=1;
}
reverse(res.begin(),res.end());
return res;
}
int main()
{
for (int i = 2023; ; i ++ )
{
bool flag = true;
string s = to_2(i);
int len = s.length();
for (int j = len - 1; j >= len - 6; j --)
{
if(s[j] != '0')
{
flag = false;
break;
}
}
if(flag)
{
cout<<i<<endl;
break;
}
}
return 0;
}
T2
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int res = 31 + 30 + 31;
bool is_run(int year)
{
if(year % 400 == 0) return true;
if(year % 100 == 0) return false;
if(year % 4 == 0) return true;
return false;
}
int main()
{
for (int i=1950;i<=2021;i++)
{
res+=365;
if(is_run(i)) res++;
}
cout<<res<<endl;
return 0;
}
T3
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int to_16(int x)
{
int res = 0;
int t=1;
while(x)
{
res+=(x%10)*t;
x/=10;
t*=16;
}
return res;
}
int main()
{
for(int i=10;i<8518;i++)
{
if(to_16(i)%i==0)
{
cout<<i<<endl;
break;
}
}
return 0;
}
T4
Sample Input
174094882455171152761423221685761892795431233411387427793198
650286024865090061389344606618496378829135984076361542097372
601657541200146071777733599818266038012509478351201640618984
143988087783837107349651099683484992553337438088068198972282
890781586124258626539246182119762952003918195325258677229419
698255491250839396799769357665825441616335532825361862146291
503649293440596342887581257444442930778730382520372975343211
325351222640703400531067500454956482168314849207060705673849
265774579830223671554026061117300483012903885770893074783710
083450145620356667677191627276513995926532444279237315785832
411595106453089134746365281031552217482363035280722591085079
053410485925413958279617719034175332412908745680774313630190
429314820559328748143552689295945058801322270313370955837837
939182801848609300876356583948397645861551964542532682663945
625356614462682551015176002433628234343684739800880514363921
982340231989891351425389287014819359798014755509282450440511
590838726938103384801541373585690893606978941566666714061214
952341523168827712604946036245881214982452998386986623826275
782780208928205527678781609589000725521486468983551558405472
149903035076783644195574734088152324666290493119955560594634
905391288186024902215444250421277955403412298227858394469856
607272647132163832860126054679347881638761723785858733108109
249157334220127702410373959720286708183036202841837581704881
367895556630088230650972282944827258473951902831431040790814
079538232104075905120989173307660289899942087873076421916033
622143260549608274076012938515668898707915863945382394851328
164677964192631597026176253407553188801750590935427267220117
591817866992665840378311257621611574856498432538327068011953
631534031790352912617015229051836886166704989498756486878095
690013558017746707412183571476823027885971347137127534455141
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 35, M = 65;
int q[N][M], f[N][M];
int main()
{
for(int i=1;i<=30;i++)
{
string s;
cin>>s;
for(int j=1;j<=60;j++)
{
q[i][j]=s[j-1]-'0';
}
}
for(int i=1;i<=30;i++)
{
for(int j=1;j<=60;j++)
{
f[i][j]=max(f[i-1][j],f[i][j-1])+q[i][j];
}
}
cout<<f[30][60]<<endl;
return 0;
}
T6
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
LL t,c,s;
int main()
{
cin>>t>>c>>s;
cout<<(s-c)*t/c;
return 0;
}
T7
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_set>
#include <vector>
using namespace std;
unordered_set<string> set;
vector<string> q;
int main()
{
int n;
cin>>n;
for (int i = 1; i <= n; i ++ )
{
string s;
cin>>s;
if(set.find(s)==set.end())
{
set.insert(s);
q.push_back(s);
}
}
for(string s : q) cout<<s<<endl;
return 0;
}
T8
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
bool check(string s)
{
int l=0,r = s.length()-1;
while(l<r)
{
if(s[l]!=s[r]) return false;
l++,r--;
}
return true;
}
int main()
{
string s;
cin>>s;
int i;
int len = s.length();
for(i=0;i<len;i++)
{
string tmp = s.substr(i,len);
if(check(tmp)) break;
}
string t=s.substr(0,i);
reverse(t.begin(),t.end());
cout<<s+t;
return 0;
}
T9
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110;
int n,m;
char a[N][N];
int dx[4] = {-1,-1, 1, 1};
int dy[4] = {-1, 1, 1,-1};
int main()
{
cin>>n>>m;
int res=0;
for (int i = 1; i <= n; i ++ )
{
string s;
cin>>s;
for (int j = 0; j < m; j ++ )
{
a[i][j+1] = s[j];
}
}
for (int i = 1; i <= n; i ++ )
{
for (int j = 1; j <= m; j ++ )
{
char ch = a[i][j];
for(int k=1;k<=50;k++)
{
bool flag=true;
for(int t=0;t<4;t++)
{
int tx = i + k * dx[t], ty = j + k * dy[t];
if(tx<1 || tx>n || ty<1 || ty>m || a[tx][ty]!=ch)
{
flag=false;
break;
}
}
if(flag) res++;
}
}
}
cout<<res;
return 0;
}
T10
从后往前维护一个升序的后缀:当遍历到x时,查询后面有几个比x小的,答案加上x*后面比x小的个数。然后将x放入树状数组,tr[x]+=1
树状数组tr[i]表示数值为i的数的个数,因为是从后往前遍历的,所以query(x)查询到的就是x后面的比x小的个数。
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1000010;
typedef long long LL;
int n;
int a[N];
int tr[N];
int lowbit(int x)
{
return x & -x;
}
void update(int x) // 位置x加1
{
for (int i = x; i <= n; i += lowbit(i)) tr[i] += 1;
}
int query(int x) // 返回前x个数的和
{
int res = 0;
for (int i = x; i; i -= lowbit(i)) res += tr[i];
return res;
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i ++ )
scanf("%d", &a[i]);
LL res=0;
for(int i=n;i>=1;i--)
{
int tmp=a[i];
res+=query(tmp-1)*1ll*tmp;
update(tmp);
}
cout<<res;
return 0;
}
好耶!T5里?