头像

喜欢敲代码的仔




离线:9小时前


活动打卡代码 AcWing 1432. 棋盘挑战

#include <bits/stdc++.h>
using namespace std;

int n;

bool stx[15], de[50], ude[50];
int path[30];
int cnt;

void dfs(int deep, int step)
{
    if (deep == n + 1 && step == n)
    {
        cnt ++ ;
        if (cnt > 3) return;


        // 输出
        for (int i = 1; i <= n; i ++ )
            cout << path[i] << " ";
        puts("");
        return;
    }
    else if (deep > n || step > n) return;
    for (int i = 1; i <= n; i ++ )
    {
        if (!stx[i] && !de[i + deep] && !ude[i - deep + n])
        {
            stx[i] = de[i + deep] = ude[i - deep + n] = true;
            path[deep] = i;
            dfs(deep + 1, step + 1);
            stx[i] = de[i + deep] = ude[i - deep + n] = false;
        }
    }
}


int main()
{
    cin >> n;
    dfs(1, 0);
    cout << cnt << endl;
    return 0;
}


活动打卡代码 AcWing 754. 平方矩阵 II

#include <bits/stdc++.h>
using namespace std;

int n;

int main()
{
    while (cin >> n, n)
    {
        for (int i = 1; i <= n; i ++ )
        {
            for (int j = i; j >= 1; j -- )
                cout << j << " " ;
            for (int j = 2; j <= n - i + 1; j ++ )
                cout << j << " " ;
            cout << endl;
        }
        cout << endl;
    }
    return 0;
}


活动打卡代码 AcWing 1532. 找硬币

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 200;
int n, m;
int w[N];

int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i ++ ) scanf("%d", &w[i]);
    sort(w, w + n);

    for (int l = 0, r = n - 1; l < r; ) {
        if (w[l] + w[r] == m)
        {
            cout << w[l] << " " << w[r] << endl;
            return 0;
        }
        else if (w[l] + w[r] > m) r -- ;
        else l ++ ;
    }
    puts("No Solution");
    return 0;
}


活动打卡代码 AcWing 1208. 翻硬币

#include <bits/stdc++.h>
using namespace std;

string s, t;
int res;

int main()
{
    cin >> s >> t;
    for (int i = 0; i < s.length(); i ++ )
        if (s[i] != t[i])
        {
            t[i] = s[i];
            t[i + 1] = t[i + 1] == '*' ? 'o' : '*';
            res ++ ;
        }
    cout << res << endl;
    return 0;
}


活动打卡代码 AcWing 429. 奖学金

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

struct P
{
    int chinese, eng, math, id;
    bool operator < (const P & p) const {
        int sum1 = chinese + eng + math;
        int sum2 = p.chinese + p.eng + p.math;
        if (sum1 != sum2) return sum1 > sum2;
        else if (chinese != p.chinese) return chinese > p.chinese;
        else return id < p.id;
    }
}p[N];

int n;

int main()
{
    cin >> n;
    for (int i = 1; i <= n; i ++ )
    {
        cin >> p[i].chinese >> p[i].math >> p[i].eng;
        p[i].id = i;
    }

    sort(p + 1, p + n + 1);

    for (int i = 1; i <= 5; i ++ )
        cout << p[i].id << " " << p[i].chinese + p[i].eng + p[i].math << endl;
    return 0;
}


活动打卡代码 AcWing 422. 校门外的树

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int N = 10010;
int L, n, res;
PII p[N];

int main()
{
    cin >> L >> n;
    res = L + 1;
    for (int i = 0; i < n; i ++ )
        scanf("%d%d", &p[i].first, &p[i].second);
    sort(p, p + n);
    int l = p[0].first, r = p[0].second;
    for (int i = 1; i < n; i ++ )
    {
        if (p[i].first <= r && p[i].second >= r) r = p[i].second;
        else if (p[i].first > r)
        {
            res -= r - l + 1;
            l = p[i].first;
            r = p[i].second;
        }
    }
    res -= r - l + 1;
    cout << res << endl;
    return 0;
}


活动打卡代码 AcWing 1227. 分巧克力

#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 20;

int n, k;
int w[N], h[N];

bool check(int x)
{
    int sum = 0;
    for (int i = 0; i < n; i ++ )
        sum += (w[i] / x) * (h[i] / x);
    return sum >= k;
}

int main()
{
    cin >> n >> k;
    for (int i = 0; i < n ;i ++ ) scanf("%d%d", &w[i], &h[i]);
    int l = 1, r = 100100;

    while (l < r)
    {
        int mid = l + r + 1 >> 1;
        if (check(mid)) l = mid;
        else r = mid - 1;
    }
    cout << r << endl;
    return 0;
}


活动打卡代码 AcWing 680. 剪绳子

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

int n, m, maxx;
int w[N];

bool check(double x)
{
    int num = 0;
    for (int i = 0; i < n; i ++ )
        num += w[i] / x;
    return num >= m;
}

int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i ++ )
    {
        scanf("%d", &w[i]);
        maxx = max(maxx, w[i]);
    }

    double l = 0, r = maxx;
    while (r - l > 1e-4)
    {
        double mid = (l + r) / 2;
        if (check(mid)) l = mid;
        else r = mid;
    }
    printf("%.2f\n", r);
    return 0;
}


活动打卡代码 AcWing 1346. 回文平方

#include <bits/stdc++.h>
using namespace std;
int k;
char nums[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'};
vector<char> dig;

bool check(int num)
{
    dig.clear();
    while (num)
    {
        dig.push_back(nums[num % k]);
        num /= k;
    }
    for (int i = 0; i < (int) dig.size(); i ++ )
        if (dig[i] != dig[dig.size() - 1 - i])
            return false;
    return true;
}

void print(int num)
{
    dig.clear();
    while (num)
    {
        dig.push_back(nums[num % k]);
        num /= k;
    }
    for (int i = (int) dig.size() - 1; i >= 0; i -- )
        cout << dig[i];
}

int main()
{
    cin >> k;
    for (int i = 1; i <= 300; i ++ )
    {
        if (check(i * i))
        {
            print(i);
            cout << " ";
            print(i * i);
            cout << endl;
        }
    }
    return 0;
}


活动打卡代码 AcWing 1113. 红与黑

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
char s[25][25];
int n, m, stx, sty;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};

int findst()
{
    for (int i = 0; i < n; i ++ )
        for (int j = 0; j < m; j ++ )
            if (s[i][j] == '@')
            {
                stx = i, sty = j;
                break;
            }
}

int bfs()
{
    int sum = 1;
    queue<PII> q;
    q.push({stx, sty});
    s[stx][sty] = '#';
    while (q.size())
    {
        PII t = q.front();
        q.pop();
        for (int i = 0; i < 4; i ++ )
        {
            int a = t.first + dx[i], b = t.second + dy[i];
            if (a >= 0 && a < n && b >= 0 && b < m && s[a][b] == '.')
            {
                sum ++ ;
                s[a][b] = '#';
                q.push({a, b});
            }
        }
    }
    return sum;
}

int main()
{
    while (scanf("%d%d", &m, &n), n || m)
    {
        for (int i = 0; i < n; i ++ ) scanf("%s", s[i]);
        findst();
        cout << bfs() << endl;
    }
    return 0;
}