牛客小白月赛26

题目链接:https://ac.nowcoder.com/acm/contest/6013
题解链接:https://ac.nowcoder.com/discuss/442540

B - 牛牛爱数学

没有开long long,WA了一发

 #include <iostream>
using namespace std;
typedef long long ll;

int main(void)
{
    ll t, a, b, c, d;
    cin >> t;
    while (t--){
        cin >> a >> b >> c;
        if ((b * c) % a == 0)
            cout << (b * c) / a << endl;
        else
            cout << -1 << endl;
    }
    
    return 0;
}

E - 牛牛走迷宫

bfs+路径记录

#include <bits/stdc++.h>
using namespace std;
const int N = 505;

int n, m;
char ma[N][N];
bool vis[N][N];
int dx[] = {1, 0, 0, -1}, dy[] = {0, -1, 1, 0};
typedef pair<int, int> P;
P pre[N][N];

void bfs()
{
	queue<P> que;
	que.push(P(0, 0));
	while (que.size())
	{
		P p = que.front(); que.pop();
		for (int i = 0; i < 4; i++)
		{
			int nx = p.first + dx[i], ny = p.second + dy[i];
			if (nx >= 0 && nx < n && ny >= 0 && ny < m && ma[nx][ny] == '0' && vis[nx][ny] == 0)
			{
				pre[nx][ny] = P(p.first, p.second);
				que.push(P(nx, ny));
				vis[nx][ny] = 1;
			}
		}
	}
}
int main(void)
{
    cin >> n >> m;
	for (int i = 0; i < n; i++)
		cin >> ma[i];
    
	bfs();
    if (vis[n - 1][m - 1] == 0){
        cout << -1 << endl;
        return 0;
    }
    
	stack<P> stk;
	P p(n - 1, m - 1);
	stk.push(p);
	while (p.first != 0 || p.second != 0){
		p = pre[p.first][p.second];
		stk.push(p);
	}
    
    int step = stk.size() - 1;
    cout << step << endl;
    
	P a, b;
	while (stk.size() - 1){
		a = stk.top(); stk.pop();
		b = stk.top();
		if (b.first > a.first)
			cout << "D";
		else if (b.first < a.first)
			cout << "U";
		else if (b.second > a.second)
			cout << "R";
		else if (b.second < a.second)
			cout << "L";
	}
	cout << endl;
    
	return 0;
}

G - 牛牛爱几何

#include <bits/stdc++.h>
using namespace std;
const double pi = 3.1415926535;

int main(void)
{
    double l, res;
    while (cin >> l){
        res = l * l * (pi / 2 - 1);
        printf("%.6lf\n", res);
    }
    
    return 0;
}

I - 恶魔果实

dfs

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M = 1e4 + 7;

bool t[10][10], vis[10];
ll ret;

void dfs(int u)
{
    for (int i = 0; i < 10; i++){
        if (t[u][i] == 1 && vis[i] == 0){
            vis[i] = 1;
            ret++;
            dfs(i);
        }
    }
}

int main(void)
{
    string x;
    int n, a, b;
    cin >> x >> n;
    while (n--){
        cin >> a >> b;
        if (a != b)
            t[a][b] = 1;
    }
    ll res = 1;
    for (int i = 0; i < x.size(); i++){
        ret = 1;
        memset(vis, 0, sizeof vis);
        vis[x[i] - '0'] = 1;
        dfs(x[i] - '0');
        res = (res * ret) % M;
    }
    cout << res << endl;
    
    return 0;
}

J - 牛牛喜欢字符串

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;

char s[N];
int a[30];

int main(void)
{
    int n, k, res = 0;
    int maxv;
    cin >> n >> k;
    cin >> s;
    for (int i = 0; i < k; i++){
        maxv = -1;
        memset(a, 0, sizeof a);
        for (int j = i; j < n; j += k){
            a[s[j] - 'a']++;
        }
        for (int j = 0; j < 26; j++){
            maxv = max(maxv, a[j]);
        }
        res += n / k - maxv;
    }
    cout << res << endl;
    
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值