L1-006 连续因子

#include <bits/stdc++.h>
#define sc(x) scanf("%lld", &(x))
#define pr(x) printf("%lld\n", x)
using namespace std;
typedef long long ll;
const int N = 5e5 + 7;
const ll mod = 1e9 + 7;
int fact[N];
int main() {
    ll x;
    while (~sc(x)) {
        int cnt = 0;
        for (int i = 2, sx = sqrt(x) + 1; i <= sx; ++i)
            if (x % i == 0) fact[++cnt] = i;
        if (cnt == 0) {
            printf("1\n%lld\n", x);
            continue;
        }
        vector<int> ans;
        for (int i = 1; i <= cnt; ++i) {
            vector<int> now;
            for (ll t = x, j = fact[i]; t > 1; ++j) {
                if (t % j != 0) break;
                now.push_back(j), t /= j;
            }
            if (now.size() > ans.size()) ans = now;
        }
        int n = ans.size();
        printf("%d\n", n);
        for (int i = 0; i < n; ++i)
            printf("%d%c", ans[i], i == n - 1 ? '\n' : '*');
    }
    return 0;
}

N个数求和

#include <bits/stdc++.h>
#define sc(x) scanf("%lld", &(x))
typedef long long ll;
const int N = 1e2 + 7;
ll a[N], b[N], n;
inline ll lcm(ll a, ll b) { return a / __gcd(a, b) * b; }
int main() {
    sc(n);
    for (int i = 1; i <= n; ++i) scanf("%lld/%lld", &a[i], &b[i]);
    for (int i = 1; i <= n; ++i) {
        ll g = __gcd(a[i], b[i]);
        a[i] /= g, b[i] /= g;
    }
    ll L = 1;
    for (int i = 1; i <= n; ++i) L = lcm(L, b[i]);
    for (int i = 1; i <= n; ++i) a[i] *= (L / b[i]);
    ll sum = 0;
    for (int i = 1; i <= n; ++i) sum += a[i];
    ll z = sum / L, r = sum % L, g = __gcd(r, L);
    r /= g, L /= g;
    if (!z && !r) putchar('0');
    if (z) printf("%lld", z);
    if (z && r) putchar(' ');
    if (r) printf("%lld/%lld", r, L);
    return 0;
}

判断素数

#include <bits/stdc++.h>
#define sc(x) scanf("%d", &(x))
#define pr(x) printf("%lld\n", x)
using namespace std;
typedef long long ll;
bool chk(int n) {
    if (n == 1) return 0;
    if (n == 2) return 1;
    ll a = sqrt(n) + 1;
    for (int i = 2; i <= a; ++i)
        if (n % i == 0) return 0;
    return 1;
}
int main() {
    int n, T;
    sc(T);
    while (T--) sc(n), puts(chk(n) ? "Yes" : "No");
    return 0;
}

A+B

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int deal(string s) {
    int n = s.length();
    if (n > 4) return -1;
    for (int i = 0; i < n; ++i)
        if (s[i] > '9' || s[i] < '0') return -1;
    int ans = 0;
    for (int i = 0; i < n; ++i) ans = ans * 10 + s[i] - '0';
    if (ans == 0 || ans > 1000) return -1;
    printf("%d", ans);
    return ans;
}
int main() {
    string s, a, b;
    while (getline(cin, s)) {
        int blankpos = s.find(" ");
        a = s.substr(0, blankpos);
        b = s.substr(blankpos + 1);
        int ra = deal(a);
        if (ra == -1) putchar('?');
        printf(" + ");
        int rb = deal(b);
        if (rb == -1) putchar('?');
        printf(" = ");
        if (ra != -1 && rb != -1)
            printf("%d", ra + rb);
        else
            putchar('?');
        putchar(10);
    }
}

L1-043 阅览室

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int getd(int a, int x, int b, int y) { return (b - a) * 60 + (y - x); }
int main() {
    int n;
    scanf("%d", &n);
    vector<int> out, aver;
    unordered_map<int, pair<int, int>> tm;  //借书时间
    while (n) {
        int num, h, m;
        char op;
        scanf("%d %c %d:%d", &num, &op, &h, &m);
        if (num == 0) {
            ll sum = 0;
            for (int &x : aver) sum += x;
            int an = aver.size();
            double avetime = an ? (sum * 1.0 / an) : 0;
            printf("%d %.0lf\n", an, avetime);
            --n;
            out.clear();
            aver.clear();
            tm.clear();
        } else if (op == 'S') {                                    //借书
            if (find(out.begin(), out.end(), num) == out.end()) {  //没有被借
                out.push_back(num);
                tm[num] = make_pair(h, m);
            }else tm[num] = make_pair(h, m);
        } else {                                                   //还书
            if (find(out.begin(), out.end(), num) != out.end()) {  //借过
                out.erase(find(out.begin(), out.end(), num));
                int sh = tm[num].first, sm = tm[num].second;
                aver.push_back(getd(sh, sm, h, m));
            }
        }
    }
}

本题题意表述非常不清晰,如果出现连续两次,两个人借同一本书,认后不认前。

但题面并没有表述清楚这一逻辑。