思路乱套
查看原帖
思路乱套
1041365
Sirius6699楼主2024/7/14 15:41

代码思路已经乱了,求调:

// #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define coutdouble(a, b) cout << fixed << setprecision(a) << (b)
#define ll long long
#define endl '\n'

using namespace std;

const int N = 1e5 + 5;
const int INF = 0x3f3f3f;
const int INFS = -0x3f3f3f;

int k, m = 0;

void cnt(int x, int num, int bed) // x:第几个人 num:找最小的m,bad:坏人数量
{
    if (bed == 0) // 如果坏人死光,更新m
    {
        m = num;
        return ;
    }
    else
    {
        if (x > k * 2) x = 1; // 如果数完一圈,回到第一人
        if (x > k) cnt(x++, num, bed--); // 如果当前的人是坏人(前k人是好人,编号大于k的就是坏人)
        else cnt(1, num++, k); // 如果当前的人是好人(编号小于等于k),重来
    }
}

void solve()
{
    cin >> k;
    cnt(1, 1, k);
    cout << m << endl;
}

signed main()
{
    // freopen("../in_out/in.txt", "r", stdin);
    // freopen("../in_out/out.txt", "w", stdout);

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);  // 关闭同步流

    int t = 1;
    // cin>>t;
    while (t--)
    {
        solve();
    }

    return 0;
}

2024/7/14 15:41
加载中...