PTA甲 1058~1060题解

1058 A+B in Hogwarts

类似高精度的模拟进位

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

int main()
{
    int a, b, c, A, B, C;
    scanf("%d.%d.%d %d.%d.%d", &a, &b, &c, &A, &B, &C);

    int z = c + C, up = 0;
    up = z / 29;
    z %= 29;

    int y = b + B + up;
    up = y / 17;
    y %= 17;

    int x = a + A + up;
    cout << x << "." << y << "." << z << '\n';

    return 0;
}

1059 Prime Factors

质因数分解 long int就是int 差点以为是long long
对于一个数 x x x,至多有一个质因子比 x \sqrt{x} x 大,所以只需枚举到 x \sqrt{x} x ,此时剩下的如果不是1,那就一定是那个 > x \gt \sqrt{x} >x 的质因子了。
可以在埃氏筛的同时,分解这个 x x x,时间复杂度大约为 O ( x ⋅ log ⁡ log ⁡ x ) O(\sqrt{x} \cdot \log{\log{\sqrt{x}}}) O(x loglogx )

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

using i64 = long long;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);

    int n; cin >> n;
    if (n == 1) {
        cout << "1=1\n";
        return 0;
    }
    cout << n << "=";
    map<int, int> cnt;
    array<int, 100010> not_prime{};

    int up = sqrt(n);
    for (int i = 2; i <= up; i++) {
        if (!not_prime[i]) {
            while (n % i == 0) {
                cnt[i]++;
                n /= i;
            }
            if (n == 1) break;
            for (int j = i + i; j <= up; j += i) not_prime[j]++;
        }
    }

    if (n ^ 1) cnt[n]++;

    string res;
    for (const auto& [a, b] : cnt) {
        if (b == 1) {
            res += to_string(a) + "*";
        } else {
            res += to_string(a) + "^" + to_string(b) + "*";
        }
    }

    cout << res.substr(0, res.size() - 1) << '\n';

    return 0;
}

1060 Are They Equal

垃圾模拟题 b u l l s h i t bullshit bullshit

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

using i64 = long long;

int n;

string solve(const string& ss) {
    // 消除前导0
    string s;
    int d = -1;
    for (int i = 0; i < ss.size(); i++) {
        if (ss[i] ^ '0') {
            d = i;
            break;
        }
    }
    if (d == -1) s = "0";
    else s = ss.substr(d);
    if (s[0] == '.') s.insert(s.begin(), '0');

    bool isZero = true;
    for (char c : s) {
        if (c != '.' && c != '0') isZero = false;
    }
    if (isZero) s = "0";
    
    string res = "0.";
    if (s.find('.') ^ string::npos) {
        // 0.01 0.12类似的情况
        if (s[0] == '0') {
            int sz = int(s.size()) - 2, idx = 2, e = 0;
            for (int i = 2; i < s.size(); i++) {
                if (s[i] == '0') {
                    --sz;
                    idx++;
                    --e;
                } else break;
            }
            if (sz < n) {
                res += s.substr(idx);
                for (int i = 0; i < n - sz; i++) res += "0";
            } else {
                res += s.substr(idx, n);
            }
            res += "*10^" + to_string(e);
        } else {
            // 1.2 的情况
            int x = s.find('.');
            auto sz = s.size();
            if (sz - 1 < n) {
                for (char c : s) {
                    if (c ^ '.') res += c;
                }
                for (int i = 0; i < n - sz + 1; i++) res += "0";
            } else {
                int cnt = 0;
                for (char c : s) {
                    if (c ^ '.') {
                        ++cnt;
                        res += c;
                        if (cnt == n) break;
                    }
                }
            }
            res += "*10^" + to_string(x);
        }
    } else {
        // 整数的情况
        auto sz = s.size();
        if (sz < n) {
            for (char c : s) res += c;
            for (int i = 0; i < n - sz; i++) res += "0";
        } else {
            for (int i = 0; i < n; i++) res += s[i];
        }
        res += "*10^" + (s[0] == '0' ? "0" : to_string(sz));
    }
    return res;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);

    string t, s;
    cin >> n >> t >> s;

    auto s1 = solve(t), s2 = solve(s);

    if (s1 == s2) {
        cout << "YES " << s1 << '\n';
    } else {
        cout << "NO " << s1 << ' ' << s2 << '\n';
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值