第二题:年号字符
解法一:暴力
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<string>v;
string temp="A";
for(int i=0;i<26;i++)
{
temp[0]='A'+i;
v.push_back(temp);
}
temp="AA";
for(int i=0;i<26;i++)
{
temp[0]='A'+i;
for(int j=0;j<26;j++)
{
temp[1]='A'+j;
v.push_back(temp);
}
}
temp="AAA";
for(int i=0;i<26;i++)
{
temp[0]='A'+i;
for(int j=0;j<26;j++)
{
temp[1]='A'+j;
for(int k=0;k<26;k++)
{
temp[2]='A'+k;
v.push_back(temp);
}
}
}
cout<<v[2018]<<endl;
return 0;
}
解法二:
#include<bits/stdc++.h>
using namespace std;
int main()
{
string str="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int n=2019;
string ans;
while(n)
{
int t=n%26;
ans+=str[t-1];
n/=26;
}
reverse(ans.begin(),ans.end());
cout<<ans;
return 0;
}
第三题:数列求值
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll a=1,b=1,c=1;
ll t=0;
for(ll i=4;i<=20190324;i++)
{
t=(a+b+c)%10000;
a=b;
b=c;
c=t;
}
cout<<t;
return 0;
}
第四题:数的分解
#include<bits/stdc++.h>
using namespace std;
bool check(int n)
{
while(n)
{
int t=n%10;
if(t==2||t==4) return false;
n/=10;
}
return true;
}
int main()
{
int ans=0;
for(int i=1;i<2019;i++)
{
for(int j=i+1;j<2019;j++)
{
int k=2019-i-j;
if(k<=j) continue;
if(check(k)&&check(i)&&check(j)) ans++;
}
}
cout<<ans<<endl;
return 0;
}
第五题:迷宫
解法一:
#include<bits/stdc++.h>
using namespace std;
string mp[35];
int vis[35][55];
int d[4][2]={{1,0},{0,-1},{0,1},{-1,0}};
string str[]={"D","L","R","U"};
struct node{
int x,y;
string s;
};
void bfs(int x,int y)
{
node start;
queue<node>q;
start.x=x;
start.y=y;
start.s="";
vis[x][y]=1;
q.push(start);
while(!q.empty())
{
node now=q.front();
q.pop();
if(now.x==29&&now.y==49)
{
cout<<now.s<<endl;
return;
}
for(int i=0;i<4;i++)
{
int nx=now.x+d[i][0];
int ny=now.y+d[i][1];
if(nx>=0&&nx<30&&ny>=0&&ny<50)
{
if(mp[nx][ny]=='0'&&vis[nx][ny]==0)
{
node next;
next.x=nx;
next.y=ny;
next.s=now.s+str[i];
q.push(next);
vis[nx][ny]=1;
}
}
}
}
}
int main()
{
for(int i=0;i<30;i++) cin>>mp[i];
bfs(0,0);
return 0;
}
解法二
#include<bits/stdc++.h>
using namespace std;
struct node {
int x;
int y;
};
char mp[31][51];
char k[4] = { 'D','L','R','U' };
int dir[4][2] = { {1,0},{0,-1},{0,1},{-1,0} };
int vis[30][50];
char pre[31][51]; //用于查找前驱点。例如pre[x][y] = 'D',表示上一个点往下走一步到了(x,y),那么上一个点是(x-1,y)
void print_path(int x, int y) { //打印路径:从(0,0)到(29,49)
if (x == 0 && y == 0) //回溯到了起点,递归结束,返回
return;
if (pre[x][y] == 'D') print_path(x - 1, y); //回溯,往上 U
if (pre[x][y] == 'L') print_path(x, y + 1); //回溯,往右 R
if (pre[x][y] == 'R') print_path(x, y - 1);
if (pre[x][y] == 'U') print_path(x + 1, y);
printf("%c", pre[x][y]);
}
void bfs() {
node start; start.x = 0; start.y = 0;
vis[0][0] = 1; //标记起点被搜过
queue<node>q;
q.push(start); //把第一个点放进队列,开始BFS
while (!q.empty()) {
node now = q.front();
q.pop();
if (now.x == 29 && now.y == 49) {
print_path(29, 49);
//打印完整路径,从终点回溯到起点,打印出来是从起点到终点的正序
return;
}
for (int i = 0; i < 4; i++) {
node next;
next.x = now.x + dir[i][0];
next.y = now.y + dir[i][1];
if (next.x < 0 || next.x >= 30 || next.y < 0 || next.y >= 50)
continue;
if (vis[next.x][next.y] == 1 || mp[next.x][next.y] == '1')
continue;
vis[next.x][next.y] = 1;
pre[next.x][next.y] = k[i]; //记录点(x,y)的前驱
q.push(next);
}
}
}
int main() {
for (int i = 0; i < 30; i++) cin >> mp[i];
bfs();
}
解法三:dfs
#include<bits/stdc++.h>
using namespace std;
char mp[35][55];
int vis[35][55];
char path[1000];
char k[4]={'D','L','R','U'};
int minp[35][55];
int mins=100000;
int dir[4][2]={{1,0},{0,-1},{0,1},{-1,0}};
char ans[1000];
void dfs(int x,int y,int step)
{
if(step>mins) return;
if(x==29&&y==49)
{
if(step<mins)
{
mins=step;
for(int i=0;i<step;i++) ans[i]=path[i];
}
return;
}
for(int i=0;i<4;i++)
{
int nx=x+dir[i][0];
int ny=y+dir[i][1];
if(nx<0||nx>=30||ny<0||ny>=50) continue;
if(mp[nx][ny]=='1'||vis[nx][ny]==1||step+1>=minp[nx][ny]) continue;
vis[nx][ny]=1;
minp[nx][ny]=step+1;
path[step]=k[i];
dfs(nx,ny,step+1);
vis[nx][ny]=0;
}
return;
}
int main()
{
for(int i=0;i<30;i++)
{
for(int j=0;j<50;j++)
{
cin>>mp[i][j];
minp[i][j]=10000;
}
}
vis[0][0]=1;
dfs(0,0,0);
for(int i=0;i<mins;i++) cout<<ans[i];
return 0;
}
第七题:完全二叉树的权值
解法一:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int n;
cin>>n;
ll maxsum=-100005;
int minl;
ll sum=0;
int l=1;
bool isfull;
for(int i=1;i<=n;i++)
{
int t;
cin>>t;
sum+=t;
isfull=false;
if(i==(pow(2,l)-1))
{
if(sum>maxsum)
{
maxsum=sum;
minl=l;
}
l++;
isfull=true;
sum=0;
}
}
if(!isfull&&sum>maxsum) minl=l;//判断是否为满二叉树
cout<<minl<<endl;
return 0;
}
第八题:等差数列
#include<bits/stdc++.h>
using namespace std;
int a[100005];
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++) cin>>a[i];
sort(a,a+n);
int mind=0;
for(int i=1;i<n;i++) mind=__gcd(mind,a[i]-a[i-1]);
if(mind==0) cout<<n;
else cout<<(a[n-1]-a[0])/mind+1;
return 0;
}