算法笔记 上机训练实战指南 第4章 入门篇(2) --算法初步 4.4贪心 学习笔记

PAT B1023 组个最小数 (20分)

给定数字 0-9 各若干个。你可以以任意顺序排列这些数字,但必须全部使用。目标是使得最后得到的数尽可能小(注意 0 不能做首位)。例如:给定两个 0,两个 1,三个 5,一个 8,我们得到的最小的数就是 10015558。

现给定数字,请编写程序输出能够组成的最小的数。

输入格式:

输入在一行中给出 10 个非负整数,顺序表示我们拥有数字 0、数字 1、……数字 9 的个数。整数间用一个空格分隔。10 个数字的总个数不超过 50,且至少拥有 1 个非 0 的数字。

输出格式:

在一行中输出能够组成的最小的数。

输入样例:

2 2 0 0 0 3 0 0 1 0

输出样例:

10015558
#include<cstdio>
int count[10];
int main(){
    for(int i=0;i<10;i++){
        scanf("%d",&count[i]);
    }
    for(int i=1;i<10;i++){
        if(count[i]>0){
            printf("%d",i);
            count[i]--;
            break;
        }
    }
    for(int i=0;i<10;i++){
        while(count[i]!=0){
            printf("%d",i);
            count[i]--;
        }
    }
    return 0;
}
PAT A1070 Mooncake (25分)

Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types of fillings and crusts can be found in traditional mooncakes according to the region's culture. Now given the inventory amounts and the prices of all kinds of the mooncakes, together with the maximum total demand of the market, you are supposed to tell the maximum profit that can be made.

Note: partial inventory storage can be taken. The sample shows the following situation: given three kinds of mooncakes with inventory amounts being 180, 150, and 100 thousand tons, and the prices being 7.5, 7.2, and 4.5 billion yuans. If the market demand can be at most 200 thousand tons, the best we can do is to sell 150 thousand tons of the second kind of mooncake, and 50 thousand tons of the third kind. Hence the total profit is 7.2 + 4.5/2 = 9.45 (billion yuans).

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers N (≤), the number of different kinds of mooncakes, and D (≤ thousand tons), the maximum total demand of the market. Then the second line gives the positive inventory amounts (in thousand tons), and the third line gives the positive prices (in billion yuans) of N kinds of mooncakes. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the maximum profit (in billion yuans) in one line, accurate up to 2 decimal places.

Sample Input:

3 200
180 150 100
7.5 7.2 4.5

Sample Output:

9.45
#include<cstdio>
#include<algorithm>
using namespace std;
struct mooncake{
    double store;
    double money;
    double price;
}moon[1010];
bool cmp(mooncake a,mooncake b){
    return a.price > b.price;
}
int main(){ 
    int n;
    double d;
    scanf("%d%lf",&n,&d);
    for(int i=0;i<n;i++){
        scanf("%lf",&moon[i].store);
    }
    for(int i=0;i<n;i++){
        scanf("%lf",&moon[i].money);
        moon[i].price = moon[i].money / moon[i].store;
    }
    sort(moon,moon+n,cmp);
    double ans=0.0;
    for(int i=0;i<n;i++){
        if(moon[i].store <= d){
            d = d - moon[i].store;
            ans = ans + moon[i].money;
        }else{
            ans = ans + moon[i].price * d;
            break;
        }
    }
    printf("%.2f",ans);
    return 0;
}
 

猜你喜欢

转载自www.cnblogs.com/coderying/p/12238427.html
今日推荐