PTA甲 1069~1072题解

文章包含三个编程问题的解决方案:1)计算数字黑洞(1069TheBlackHoleofNumbers),通过排序和比较找到特定条件;2)按每吨价格对月饼进行排序(1070Mooncake),使用排序算法找到最大价值;3)分析演讲中的模式(1071SpeechPatterns),识别字母并计数;4)解决加油站选址问题(1072GasStation),应用Dijkstra算法找到最佳位置。
摘要由CSDN通过智能技术生成

1069 The Black Hole of Numbers

按题意模拟…

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

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

    string N; cin >> N;

    array<int, 4> a{}, b{};
    for (int i = 0; i < N.size(); i++) {
        a[i] = b[i] = N[i] - '0';
    }

    auto atoi = [](const array<int, 4>& nums) -> int {
        int res = 0;
        for (int i = 0; i < 4; i++) {
            res += pow(10, 3  - i) * nums[i];
        }
        return res;
    };

    while (true) {
        sort(a.begin(), a.end(), greater<>());
        sort(b.begin(), b.end(), less<>());
        int x = atoi(a), y = atoi(b);
        int d = x - y;
        printf("%04d - %04d = %04d\n", x, y, d);
        if (d == 6174 || d == 0) break;
        fill(a.begin(), a.end(), 0);
        fill(b.begin(), b.end(), 0);
        string s = to_string(d);
        for (int i = 0; i < s.size(); i++) a[i] = b[i] = s[i] - '0';
    }

    return 0;
}

1070 Mooncake

按 每吨多少钱 排序

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

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    
    int n, M; cin >> n >> M;
    vector<pair<double, double>> v(n);
    for (int i = 0; i < n; i++) {
        cin >> v[i].first;
    }
    for (int i = 0; i < n; i++) {
        cin >> v[i].second;
    }
    
    sort(v.begin(), v.end(), 
    [](const pair<double, double>& A, const pair<double, double>& B) -> bool {
        return A.second / A.first > B.second / B.first;
    });
    
    double res = 0;
    for (int i = 0; i < n; i++) {
        if (M < v[i].first) {
            res += double(M) / v[i].first * v[i].second;
            break;
        } else {
            M -= v[i].first;
            res += v[i].second;
        }
    }
    
    cout << fixed << setprecision(2) << res << endl;
    
    return 0;
}

1071 Speech Patterns

cpp切片不太好用,可以写个py

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

int main()
{
    string s; 
    getline(cin, s);
    
    for (int i = 0; i < s.size(); i++) {
        if (s[i] >= '0' && s[i] <= '9') {
            continue;
        } else if (s[i] >= 'a' && s[i] <= 'z') {
            continue;
        } else if (s[i] >= 'A' && s[i] <= 'Z') {
            s[i] = tolower(s[i]);
        } else s[i] = ' ';
    }
    
    stringstream ss(s);
    map<string, int> mp;
    int res = 0;
    while (ss >> s) {
        mp[s]++;
        res = max(res, mp[s]);
    }
    
    for (const auto& [a, b] : mp) {
        if (b == res) {
            cout << a << ' ' << b << '\n';
            break;
        }    
    }
    
    return 0;
}

1072 Gas Station

理解题意 A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible.先按最短距离从大到小排(也可以理解,毕竟是加油站,远一点为了安全。),然后再按平均距离从小到大排,然后再按id从小到大排。
因为是一个点到所以house距离之和,显然是单源最短路,dijkstra就可以了。
G1可以转为1001,这样映射一下。输出的时候再转回来。
还有注意四舍五入…

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

using i64 = long long;

static constexpr i64 inf = 0x3f3f3f3f3f;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    
    vector<vector<pair<int, i64>>> graph(1020);
    int N, M, K, D; cin >> N >> M >> K >> D;
    while (K--) {
        string a, b, c; cin >> a >> b >> c;
        int C = stoi(c), A, B;
        if (a[0] == 'G') {
            A = 1000 + stoi(a.substr(1));
        } else A = stoi(a);
        if (b[0] == 'G') {
            B = 1000 + stoi(b.substr(1));
        } else B = stoi(b);
        graph[A].emplace_back(B, C);
        graph[B].emplace_back(A, C);
    }
    
    auto dijkstra = [&](int start) -> vector<i64> {
        vector<i64> dist(1020, inf);
        dist[start] = 0;
        priority_queue<pair<i64, int>, vector<pair<i64, int>>, greater<>> pq;
        pq.push({0, start});
        while (!pq.empty()) {
            auto [d, u] = pq.top();
            pq.pop();
            if (d > dist[u]) continue;
            for (const auto& [v, w] : graph[u]) {
                if (d + w < dist[v]) {
                    dist[v] = d + w;
                    pq.push({dist[v], v});
                }
            }
        }
        return dist;
    };
    
    struct GAS {
      int mix, id;
      double ave;
      explicit GAS(int _m, int _id, double _a): mix(_m), id(_id), ave(_a) {}
    };
    
    vector<GAS> res;
    for (int i = 1001; i <= 1000 + M; i++) {
        const auto& d = dijkstra(i);
        i64 dis = 0, mi = inf;
        for (int j = 1; j <= N; j++) {
            if (d[j] > D) goto ne;
            dis += d[j];
            mi = min(d[j], mi);
        }
        res.emplace_back(mi, i, round(double(dis) / double(N) * 10) / 10);
        ne: continue;
    }
    
    sort(res.begin(), res.end(), [](const GAS& A, const GAS& B) -> bool {
        if (A.mix == B.mix) {
            if (A.ave == B.ave) return A.id < B.id;
            return A.ave < B.ave;
        } 
        return A.mix > B.mix;
    });
    
    if (res.empty()) {
        cout << "No Solution\n";
    } else {
        const auto& r = res[0];
        cout << "G" + to_string(r.id - 1000) + "\n";
        cout << fixed << setprecision(1) << double(r.mix);
        cout << ' ' << fixed << setprecision(1) << r.ave;
    }
    
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值