【补题】AcWing暑假每日一题2022

【补题】AcWing暑假每日一题2022

4268. 性感素数

4268. 性感素数

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

int n;

bool is_prime(int x) {
    if (x < 2) return false;
    for (int i = 2; i <= x / i; i++) {
        if (!(x % i)) return false;
    }
    return true;
}

int another() {
    for (int i = n + 1;; i++) {
        if (is_prime(i) && (is_prime(i - 6) || is_prime(i + 6))) {
            return i;
        }
    }
}

int main() {
    cin >> n;
    if (is_prime(n) && (is_prime(n - 6) || is_prime(n + 6))) {
        puts("Yes");
        if (is_prime(n - 6)) {
            cout << n - 6 << endl;
        } else {
            cout << n + 6 << endl;
        }
    } else {
        puts("No");
        cout << another() << endl;
    }
    return 0;
}

4269. 校庆

4269. 校庆

#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <unordered_map>

using namespace std;
int n, m, cnt;

unordered_map<string, bool> map;


int main() {
    cin >> n;
    string a, ans1, ans2;
    int d = 99999999, d1 = 999999999;
    for (int i = 0; i < n; i++) {
        cin >> a;
        map[a] = true;
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
        cin >> a;
        if (map[a]) {
            cnt++;
            if (d1 > stoi(a.substr(6, 8))) {
                d1 = stoi(a.substr(6, 8));
                ans2 = a;
            }
        }
        if (cnt == 0) {
           if (d > stoi(a.substr(6, 8))) {
                d = stoi(a.substr(6, 8));
                ans1 = a;
            } 
        }
    }
    cout << cnt << endl;
    if (cnt == 0) cout << ans1 << endl;
    else cout << ans2 << endl;
    return 0;
} 

4273. 链表合并

4273. 链表合并

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

typedef pair<int, int> PII;

const int N = 100010;

int v[N], ne[N];
int h1, h2, n;

vector<PII> a, b;

int main() {
    cin >> h1 >> h2 >> n;
    while (n--) {
        int a, b, c;
        cin >> a >> b >> c;
        v[a] = b, ne[a] = c;
    }
    for (int i = h1; i != -1; i = ne[i]) a.push_back({i, v[i]});
    for (int i = h2; i != -1; i = ne[i]) b.push_back({i, v[i]});
    
    if (a.size() < b.size()) swap(a, b);
    
    vector<PII> c;
    for (int i = 0, j = b.size() - 1; i < a.size(); i += 2, j--) {
        c.push_back(a[i]);
        if (i + 1 < a.size()) c.push_back(a[i+1]);
        if (j >= 0) c.push_back(b[j]);
    }
    
    for (int i = 0; i < c.size(); i++) {
        printf("%05d %d ", c[i].first, c[i].second);
        if (i + 1 < c.size()) printf("%05d\n", c[i+1].first);
        else puts("-1");
    }
    
    return 0;
}

4274. 后缀表达式

4274. 后缀表达式

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 30;
int l[N], r[N];
bool f[N];
string v[N];

int n;

void dfs(int root) {
    putchar('(');
    if (l[root] != -1 && r[root] != -1) {
        dfs(l[root]);
        dfs(r[root]);
        cout << v[root];
    } else if (l[root] == -1 && r[root] != -1) {
        cout << v[root];
        dfs(r[root]);
    } else {
        cout << v[root];
    }
    putchar(')');
    return ;
}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> v[i] >> l[i] >> r[i];
        if (l[i] != -1) f[l[i]] = true;
        if (r[i] != -1) f[r[i]] = true;
    }
    int root = 1;
    while (f[root]) root++;
    dfs(root);
    return 0;
}

持续更新。。。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值