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;
}
posted @ 2020-09-25 16:23  童年の波鞋  阅读(139)  评论(0编辑  收藏  举报