1. 程式人生 > 實用技巧 >牛客小白月賽30(個人部分題解)

牛客小白月賽30(個人部分題解)

牛客小白月賽30

最近沉迷寫前端程式碼,現在緩過來簡單補下題

C:滑板上樓梯

math

// Author : RioTian
// Time : 20/12/07
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int main() {
    // freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    ll n, cnt = 0;
    cin >> n;
    ll ans = n / 4;
    cnt += ans * 2;
    ans = n % 4;
    if (ans < 3)
        cnt += ans;
    else
        cnt++;
    cout << cnt << endl;
}

D:GCD

素數篩反向選

// Author : RioTian
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int n;
bool isPrime[N];
int main() {
    // freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n;
    int cnt = 0;
    for (int i = 2; i <= n; i++) {
        if (!isPrime[i]) {
            cnt++;
            for (int j = i * 2; j <= n; j += i) {
                isPrime[j] = 1;
            }
        }
    }
    // cout << cnt << endl;
    cout << (n <= 3 ? -1 : cnt + 2) << endl;
}

E:牛牛的加法

模擬題

如果兩數字位數不同就先補零便於後面處理

// Author : RioTian
// Time : 20/12/07
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
string a, b, ans;
int cot = 0, len;
bool lin;
int main() {
    // freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> a >> b;
    if (a.size() > b.size()) {
        cot = a.size() - b.size();
        while (cot--)
            b = '0' + b;
    } else {
        cot = b.size() - a.size();
        while (cot--)
            a = '0' + a;
    }
    len = a.size();
    ans = "";
    lin = true;
    for (int i = 0; i < len; i++) {
        if ((a[i] - '0' + b[i] - '0') % 10 == 0 && lin)
            continue;
        lin = false;
        ans += (char)((a[i] - '0' + b[i] - '0') % 10 + '0');
    }
    if (ans.size() == 0)
        ans += "0";
    cout << ans << endl;
}

F:石子合併

模擬一下樣例1,會發現16 + 15,容易發下16為5項和,而\(15 = max(a[i]) * (n - 2)\)

// Author : RioTian
// Time : 20/12/07
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int n;
ll a[N];
int main() {
    // freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; ++i)
        cin >> a[i];
    ll cnt = 0, Max = 0;
    for (int i = 1; i <= n; ++i) {
        Max = max(Max, a[i]);
        cnt += a[i];
    }
    cout << Max * (n - 2) + cnt;
}