zoukankan      html  css  js  c++  java
  • b_pat_排成最小的数字 & 月饼(字符串拼接比较a+b<b+a)

    给定一个数组,数组中包含若干个整数,数组中整数可能包含前导 0。
    你需要将数组中的所有数字拼接起来排成一个数,并使得该数字尽可能小。

    思路
    a+b<b+a,则表示a应作为拼接结果的开头,这个想不到就没办法了...

    #include<bits/stdc++.h>
    using namespace std;
    bool cmp(string& a, string& b) {
        return a+b<b+a;
    }
    int main() {
        std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
        int n; cin>>n;
        string A[n]; for (int i=0; i<n; i++) cin>>A[i];
        sort(A, A+n, cmp);
    
        string s; for (string& t : A) s+=t; 
        while (s.size()>1 && s[0]=='0') s=s.substr(1);
        cout<<s;
        return 0;
    }
    

    Mooncake

    请你计算出可以赚到的最大利润。
    例如,共有 3 种月饼,存量分别为 180,150,100(单位:千吨),总价值分别为 7.5,7.2,4.5 (单位:十亿元),市场总需求量为 200,那么最佳销售方案是出售第二种月饼 150,第三种月饼 50,这样可获最大利润:7.2+4.5/2=9.45。

    思路
    是个人都知道买多点单价高的月饼能赚多点...

    #include<bits/stdc++.h>
    using namespace std;
    const int N=1005;
    struct node {
        double store, val, price;
    }A[N];
    int main() {
        std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
        int n; double D; cin>>n>>D;
    
        for (int i=0; i<n; i++) cin>>A[i].store;
        for (int i=0; i<n; i++) cin>>A[i].val, A[i].price=A[i].val/A[i].store;
        sort(A, A+n, [&](node a, node b) {return a.price>b.price;});
    
        double ans=0;
        for (int i=0; i<n; i++) {
            if (D>=A[i].store) {
                ans+=A[i].store*A[i].price;
                D-=A[i].store;
            } else {
                ans+=(double) D*A[i].price;
                break;
            }
        }
        printf("%.2f", ans);
        return 0;
    }
    
  • 相关阅读:
    CSP-S2020总结
    题解-P6687 论如何玩转 Excel 表格
    题解-UVA12995 【Farey Sequence】
    题解-P4159 [SCOI2009] 【迷路】
    题解-SP2916【GSS5
    102. 二叉树的层序遍历
    力扣 160 相交链表 快慢指针 双指针
    3. 无重复字符的最长子串 滑动窗口
    最大连续1的个数 III
    B树和B+树
  • 原文地址:https://www.cnblogs.com/wdt1/p/13730961.html
Copyright © 2011-2022 走看看