文章目录
编程题链接
试题 A: 平方和(暴力)(答案:2658417853,注意开longlong)
#include <iostream>
using namespace std;
bool check(int x)
{
while(x){
int t = x % 10;
if(t == 2 || t == 0 || t==1 || t == 9) return true;
x /= 10;
}
return false;
}
int main()
{
long long res = 0;
for(int i =1;i<=2019;i++)
{
if(check(i)) res += i * i;
}
cout << res << endl;
return 0;
}
试题 B: 数列求值(暴力)(答案:4659,注意数据太大提前mod)
#include <iostream>
using namespace std;
int main()
{
long long a = 1,b = 1,c = 1;
int mod = 10000;
int i = 20190323; // 这里少1
while(i -- )
{
long long t = (a + b + c) % mod;
a = b;
b = c;
c = t;
}
cout << a << endl;
return 0;
}
试题 C: 最大降雨量(构造)(思维题,求中位数的中位数,蒙的25,答案34,fuck)
试题 D: 迷宫(BFS)(方向矢量优先级,读文件freopen("maze.txt","r",stdin)
,求路径只需记录前驱节点即可,记得要设置边界!这种搜索题还是要多写)
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 55;
typedef pair<int,int> PII;
char g[55][55];
PII pre[N][N];
bool st[N][N];
vector<char> res;
int n = 30, m =50;
int dx[4] = {
1,0,0,-1}, dy[4] ={
0,-1,1,0};
void bfs(int sx,int sy)
{
queue<PII> q;
q.push({
sx,sy});
st[sx][sy] = true;
while(q.size())
{
PII t = q.front();
q.pop();
for(int i = 0;i < 4;i ++ )
{
int x = t.first + dx[i], y = t.second + dy[i];
if(x >= 0 && x < n && y >= 0 && y < m && !st[x][y] && g[x][y] != '1') // 障碍物没考虑到
{
st[x][y] = true;
q.push({
x,y});
pre[x][y] = t;
}
}
}
}
int main()
{
freopen("maze.txt","r",stdin);
for(int i=0;i<30;i++) cin >> g[i];
bfs(29,49);
pre[29][49].first = -1; // 设置边界
int x = 0,y = 0;
while(pre[x][y].first != -1)
{
int new_x = pre[x][y].first, new_y = pre[x][y].second;
char op;
if(new_x - x == 1) op = 'D';
else if(x - new_x == 1) op = 'U';
else if(new_y - y == 1) op = 'R';
else op = 'L';
res.push_back(op);
x = new_x, y = new_y;
}
// 答案:(比对)
//DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR
//DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR
for(auto c : res) cout <<c;
return 0;
}