这次比赛最后两个我太菜了,没做出来,ε=(´ο`*)))唉,太痛苦了,话不多说,先看前四个吧,等我搞懂了我会更新后两个
小红的好数
思路:输入一个字符串判断长度是否是2,然后0位是否和1位相同
#include<bits/stdc++.h>
using namespace std;
#define int long long
string s;
signed main()
{
cin>>s;
if(s.size()==2&&s[1]==s[0])
{
cout<<"Yes\n";
}
else
cout<<"No\n";
return 0;
}
小红的好数组
数据不大,纯暴力就可以,找到一段k子串中,只有一处是不对称的
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, k;
int a[1005];
signed main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int ans = 0;
for (int i = 1; i <= n - k + 1; i++) {
int cnt = 0;
for (int j = 0; j < k / 2; j++) {
if (a[i + j] != a[i + k - 1 - j]) {
cnt++;
}
}
if (cnt == 1) {
ans++;
}
}
cout << ans << "\n";
return 0;
}
小红的矩阵行走
思路:只能往下或者右走,广搜遍历路径即可,这条路径必须和起点元素相同
#include<bits/stdc++.h>
using namespace std;
#define int long long
int t;
int n, m;
int a[105][105];
int dx[2] = {0,1};
int dy[2] = {1,0};
int vis[105][105];
struct node {
int x, y;
};
queue<node> que;
bool bfs(int startX, int startY) {
int targetValue = a[startX][startY];
vis[startX][startY] = 1;
que.push({startX, startY});
while (!que.empty()) {
node current = que.front();
que.pop();
if (current.x == n && current.y == m) {
return true;
}
for (int i = 0; i < 2; i++) {
int tx = current.x + dx[i];
int ty = current.y + dy[i];
if (tx >= 1 && ty >= 1 && tx <= n && ty <= m && !vis[tx][ty] && a[tx][ty] == targetValue) {
vis[tx][ty] = 1;
que.push({tx, ty});
}
}
}
return false;
}
signed main() {
cin >> t;
while (t--) {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
if (a[1][1] != a[n][m]) {
cout << "No\n";
continue;
}
memset(vis, 0, sizeof(vis));
if (bfs(1, 1)) {
cout << "Yes\n";
} else {
cout << "No\n";
}
}
return 0;
}
小红的行列式构造
思路:既然是一个构造题,那么肯定有一部分元素是可以固定下来的
输入值为n
我自己思考的初始矩阵为,
x 1 1
1 2 1
z 1 1
矩阵运算结果为x-z=n;
我们可以假设z的值为1,那么x=z+n
当n=-1时,x为0,不和题意,所以n=-1可以加个特判,x=1,z=2;
#include<bits/stdc++.h>
using namespace std;
#define int long long
int x;
int c[4][4];
signed main()
{
cin>>x;
for(int i=1;i<=3;i++)
{
for(int j=1;j<=3;j++)
{
c[i][j]=1;
}
}
c[2][2]=2;
if(x==-1)
{
c[3][1]=2;
for(int i=1;i<=3;i++)
{
for(int j=1;j<=3;j++)
{
cout<<c[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}
c[1][1]=x+1;
for(int i=1;i<=3;i++)
{
for(int j=1;j<=3;j++)
{
cout<<c[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}