牛客小白月赛26

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

文章目录

  • B - 牛牛爱数学
  • E - 牛牛走迷宫
  • G - 牛牛爱几何
  • I - 恶魔果实
  • J - 牛牛喜欢字符串

B - 牛牛爱数学

没有开long long,WA了一发

 #include 
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 
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 
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 
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 
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;
}

你可能感兴趣的:(#,一些比赛题目)