牛客周赛 Round 55

目录

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();
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值