目录
A-小红的字符串_牛客周赛 Round 55 (nowcoder.com)
B-小红的序列乘积_牛客周赛 Round 55 (nowcoder.com)
C-小红的数组重排_牛客周赛 Round 55 (nowcoder.com)
D-虫洞操纵者_牛客周赛 Round 55 (nowcoder.com)
A-小红的字符串_牛客周赛 Round 55 (nowcoder.com)
思路:
计算一下即可
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#define int long long
using namespace std;
const int N = 1e6 + 10;
void slove()
{
string s;
cin >> s;
int ans = 2;
if (s[0] == s[1])
ans--;
if (s[1] == s[2])
ans--;
if (s[0] == s[2])
ans--;
if(ans==-1)
cout<<0;
else
cout<<ans;
}
signed main()
{
int t=1;
//cin >> t;
while (t--)
{
slove();
}
}
B-小红的序列乘积_牛客周赛 Round 55 (nowcoder.com)
思路:
问的只是个位数,每次计算对10取模即可
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#include<cmath>
#define int long long
using namespace std;
const int N = 1e6 + 10;
int a[N];
void slove()
{
int n;
cin >> n;
int ans = 0;
a[0] = 1;
for (int i = 1; i <= n; i++)
{
int x;
cin >> x;
x = x % 10;
x *= a[i - 1];
x = x % 10;
a[i] = x;
if (x == 6)
ans++;
}
cout << ans;
}
signed main()
{
int t = 1;
//cin >> t;
while (t--)
{
slove();
}
}
C-小红的数组重排_牛客周赛 Round 55 (nowcoder.com)
思路:
要求为a1×a2<a2×a3<⋯<an−1×an
化简一下即为a1<a3<a5,所以发现只要没有三个重复的数,即可实现,但是还需要特别注意0出现两次就不可以,所以记录数出现的次数即可。
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#include<cmath>
#define int long long
using namespace std;
const int N = 1e6 + 10;
int a[N];
map<int, int>mp;
bool cmp(int a, int b)
{
return a < b;
}
void slove()//a1<a3,a2<a4
{
int n;
int flag = 1;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
mp[a[i]]++;
if (a[i] == 0 && mp[a[i]] == 2)
flag = 0;
else if (a[i] != 0 && mp[a[i]] == 3)
flag = 0;
}
if (flag == 0)
cout << "NO\n";
else
{
cout<<"YES\n";
sort(a + 1, a + 1 + n, cmp);
for (int i = 1; i <= n; i++)
cout << a[i]<<" ";
}
}
signed main()
{
int t = 1;
//cin >> t;
while (t--)
{
slove();
}
}
D-虫洞操纵者_牛客周赛 Round 55 (nowcoder.com)
思路:
bfs走一遍,遇到墙的时候,往反方向走到墙即可
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#define int long long
using namespace std;
const int N = 1e3 + 10;
int a[N][N], vis[N][N],n;
int dx[4] = { 0,1,0,-1 };
int dy[4] = { 1,0,-1,0 };
struct node
{
int x, y, d;
};
void bfs()
{
queue<node>q;
q.push({ 1,1,0 });
memset(vis, 0, sizeof(vis));
vis[1][1] = 1;
while (!q.empty())
{
int x = q.front().x, y = q.front().y, d = q.front().d;
q.pop();
if (x == n && y == n)
{
cout << d;
return;
}
for (int i = 0; i < 4; i++)
{
int tx = x + dx[i], ty = y + dy[i];
if (vis[tx][ty] == 1)
continue;
if (a[tx][ty] == 1)
{
if (i == 0)//往右走时
{
for (int j = y; j >= 0; j--)//往反方向走遇到墙为止
{
if (a[x][j] == 1)
{
tx = x, ty = j + 1;
break;
}
}
}
else if (i == 1) //往下走时
{
for (int j = x; j >= 0; j--) //往反方向走遇到墙为止
{
if (a[j][y] == 1)
{
tx = j + 1, ty = y;
break;
}
}
}
else if (i == 2) //往左走
{
for (int j = y; j <= n + 1; j++) //往反方向走遇到墙为止
{
if (a[x][j] == 1)
{
tx = x, ty = j - 1;
break;
}
}
}
else//往上走时
{
for (int j = x; j <= n + 1; j++) //往反方向走遇到墙为止
{
if (a[j][y] == 1)
{
tx = j - 1, ty = y;
break;
}
}
}
if (vis[tx][ty] == 1)
continue;
vis[tx][ty] = 1;
q.push({ tx,ty,d+1 });
}
else
{
vis[tx][ty] = 1;
q.push({ tx,ty,d+1 });
}
}
}
cout << -1;
}
void slove()
{
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
{
cin >> a[i][j];
}
for (int i = 0; i <= n; i++)
a[i][0] = 1, a[0][i] = 1, a[n+1][i] = 1, a[i][n+1] = 1;
bfs();
}
signed main()
{
int t = 1;
//cin >> t;
while (t--)
{
slove();
}
}