- 温张鑫 的博客
七月day8
- @ 2025-7-22 20:50:15
T1 一千万零一只斑点狗
考试状况:AC
思路:可以推一个算式,因为有26个字母,但是题目要求从a到z,所以算式是:'a' + (i - 1) % 26。知道了算式我们就可以想该怎么实现了,下面是具体过程: 1.根据题目所给的数据范围可知是long long。
2.定义一个字符串存储最后的结果string ans
3.用一个循环来转换
4.要注意算式里要用i-1来取模26
5.然后用ans.insert()将字符添加到结果字符串的开头,不然输出的结果就是反的
6.最后输出我们定义的字符串ans即可
代码:
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main()
{
freopen("dog.in","r",stdin);
freopen("dog.out","w",stdout);
ll n;
cin>> n;
string ans;//存储最后的结果
for(ll i = n;i > 0;i = (i - 1)/26 )
{ //转换
char x = 'a' + (i - 1) % 26;//i-1能让结果是'a'-->'z'
ans.insert(0,1,x); //将字符添加到结果字符串的开头
}
cout<< ans << endl;
return 0;
}
T2 金字塔
思路:本题需要用到枚举和模拟 1.枚举所有可能的中心坐标:由于题目限制,中心坐标在0到100之间,我们可以枚举这个范围里的所有整数坐标。
2.我们可以对每个枚举出的坐标进行假设。
3.验证:检查所有观测点的高度是否与假设的金字塔高度一致。
代码:
#include<bits/stdc++.h>
using namespace std;
int x[110],y[110],h[110];
int main()
{
freopen("tower.in","r",stdin);
freopen("tower.out","w",stdout);
int n;
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> x[i] >> y[i] >> h[i];
if(h[i] > 0)
{
swap(x[i], x[1]);
swap(y[i], y[1]);
swap(h[i], h[1]);
}
}
for(int cx = 0; cx <= 100; cx++)
{
for(int cy = 0; cy <= 100; cy++)
{
int H = h[1] + abs(cx - x[1]) + abs(cy - y[1]);
bool flag = true;
for(int i = 1; i <= n; i++)
{
int m = max(H - abs(cx - x[i]) - abs(cy - y[i]), 0);
if(m != h[i])
{
flag = false;
break;
}
}
if(flag)
{
cout << cx << " " << cy << " " << H << endl;
return 0;
}
}
}
return 0;
}
T3 走迷宫
思路:用bfs来做 代码:
#include<bits/stdc++.h>
using namespace std;
int n,m,mx=0;
char a[22][22],st[22][22];
int dx[10] = {1,0,-1,0};
int dy[10] = {0,1,0,-1};
struct P{
int x, y, z;
};
int bfs(int qx,int qy,int zx,int zy){
queue<P>q;
q.push({qx,qy,0});
st[qx][qy]=1;
while(q.size()){
P t = q.front();
q.pop();
if(t.x == zx && t.y == zy) return t.z;
int x=t.x,y=t.y,z=t.z;
for(int i = 0;i < 4;i++){
int xx = x+dx[i];
int yy = y+dy[i];
if(xx<1 || xx>n || yy<1 || yy>m) continue;
if(a[xx][yy] == '#') continue;
if(st[xx][yy] == 1) continue;
st[xx][yy] = 1;
q.push({xx,yy,z+1});
}
}
return -1;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
freopen("maze.in","r",stdin);
freopen("maze.out","w",stdout);
cin>> n >> m;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
cin>> a[i][j];
}
}
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
for(int ii = 1;ii <= n;ii++){
for(int jj = 1;jj <= m;jj++){
if(a[i][j] =='#' || a[ii][jj] == '#') continue;
memset(st,0,sizeof st);
int z = bfs(i,j,ii,jj);
mx = max(mx,z);
}
}
}
}
cout<< mx;
return 0;
}
T4 划分问题
思路: 1.建立一个动态数组 2.对数组进行排序 3.用中间的两个数进行减法,结果就是它们的差值
代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
freopen("hf.in","r",stdin);
freopen("hf.out","w",stdout);
int n;
cin >> n;
vector<int> x(n);//动态数组
for (int i = 0; i < n; i++)
{
cin >> x[i];
}
sort(x.begin(), x.end()); // 对数组进行排序
int m = n / 2;
if (x[m-1] < x[m]) {//算式
cout << x[m] - x[m - 1] << endl;
} else {
cout << 0 << endl;
}
return 0;
}