首页 > 试题广场 >

月饼 (25)

[编程题]月饼 (25)
  • 热度指数:33700 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需
求量,请你计算可以获得的最大收益是多少。

注意:销售时允许取出一部分库存。样例给出的情形是这样的:假如我们有3种月饼,其库存量分别为18、15、10万吨,总售价分别为75、
72、45亿元。如果市场的最大需求量只有20万吨,那么我们最大收益策略应该是卖出全部15万吨第2种月饼、以及5万吨第3种月饼,获得
72 + 45/2 = 94.5(亿元)。

输入描述:
每个输入包含1个测试用例。每个测试用例先给出一个不超过1000的正整数N表示月饼的种类数、以及不超过500(以万吨为单位)的正整数
D表示市场最大需求量。随后一行给出N个正数表示每种月饼的库存量(以万吨为单位);最后一行给出N个正数表示每种月饼的总售价(以亿
元为单位)。数字间以空格分隔。


输出描述:
对每组测试用例,在一行中输出最大收益,以亿元为单位并精确到小数点后2位。
示例1

输入

3 20<br/>18 15 10<br/>75 72 45

输出

94.50
#include<stdlib.h>
#include<iostream>
using namespace std;

class moon {
public:
    int number;
    int profit;
    double sin;
    double single() {
        double temp = 1.0 * profit / (1.0*number);
        return temp;
    }
};
void buble(moon array[1000],int N) {
    for (int i = 0; i < N - 1; i++) {
        for (int j = 0; j < N - i - 1; j++) {
            if (array[j].sin < array[j + 1].sin) {
                moon temp = array[j];
                array[j] = array[j + 1];
                array[j + 1] = temp;

            }
        }
    }
}
int main() {
    int N;
    int all;
    cin >> N >> all;
    moon array[1000];
    int s = 0;
    for (int i = 0; i < N; i++) {
        cin >> array[i].number ;
    }
    for (int i = 0; i < N; i++) {
        cin >> array[i].profit;
        array[i].sin = array[i].single();
    }
    buble(array, N);
    int number1 = 0;
    double profit1 = 0;
    int count = -1;
    while (all - number1 > 0) {
        count++;
        number1 += array[count].number;
        
    }
    int cha = array[count].number -number1+all;
    for (int k = 0; k < count; k++) {
        profit1 += array[k].profit;
    }
    profit1 += array[count].sin * cha;
    printf("%0.2f",profit1);
}
发表于 2021-11-29 13:10:27 回复(2)
刚接触在线编程,很多地方还处理不好。以下这段代码,PTA给出的错误提示是 返回为零
我自己用的是Idea,没有出现错误,运行结果也正确,而且因为这个错误好像是跟语法有关,所以
也没有测试点的错误提示,希望有常常在网上编程的同学能帮我答疑解惑。

为了方便大家快速了解代码结构,我简述一下思路:
    两个数组,分别调用一个方法,把控制台的输入依次存入数组。之后两个数组调用排序方法,
    根据单价的不同,进行冒泡排序(两个数组同时排序,可以保证,库存和价格的索引一直相同)。
     之后就是if语句去循环判断是否达到总需求量,并依次记录利润值了,最后返回利润值,并输出。



import java.util.*;
public class Main {
    public double doTest(){
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();//得到月饼的总的种类数
        int D = sc.nextInt();//得到市场最大需求量
        double stoke[] = new double[N];
        double price[] = new double[N];
        double profit = 0;//利润总额
        input(stoke);
        input(price);
        getUnitPrice(stoke,price);//按照单价升序排列的数组
        /*for(double x:stoke){
            System.out.println("here"+x);
        }*/
        for(int i=stoke.length-1;i>=0;i--){
            if(stoke[i]<D){
                profit += price[i];//72+
                D -= stoke[i];
            }
            else{
                profit += D * (price[i]/stoke[i]);
                break;
            }
        }
        return profit;
    }

    public void getUnitPrice(double array1[],double array2[]){
        for(int i=0;i<array1.length-1;i++) {
            for (int j = 0; j < array1.length - i - 1; j++) {
                if (array2[j] / array1[j] > array2[j + 1] / array1[j + 1]) {
                    double temp = array1[j];
                    array1[j] = array1[j + 1];
                    array1[j + 1] = temp;
                    temp = array2[j];
                    array2[j] = array2[j + 1];
                    array2[j + 1] = temp;
                }
            }
        }
    }

    public void input(double temp[]){
        Scanner sc = new Scanner(System.in);
        for(int i=0;i<temp.length;i++){
            temp[i] = sc.nextInt();
        }
    }

    public static void main(String []args){
        Main ws = new Main();
        double profit = ws.doTest();
        System.out.println(String.format("%.2f",profit));
    }
}

发表于 2020-04-09 21:22:28 回复(0)
#include <iostream>
(720)#include <algorithm>
using namespace std;

struct MoonCake
{
    double stock;
    double total_price;
    static bool cmp(const MoonCake& a, const MoonCake& b) { return (a.total_price / a.stock) > (b.total_price / b.stock); }
};

int main()
{
    long long int N, sum;
    double total = 0;

    scanf("%lld %lld", &N, &sum);

    MoonCake moonCakes[N];

    for (int i = 0; i < N; ++i) {
        scanf("%lf", &moonCakes[i].stock);
    }

    for (int i = 0; i < N; ++i) {
        scanf("%lf", &moonCakes[i].total_price);
    }

    sort(moonCakes, moonCakes + N, MoonCake::cmp);

    for (int i = 0; i < N; ++i) {
        if (sum <= moonCakes[i].stock) {
            total += (sum / moonCakes[i].stock * moonCakes[i].total_price);
            break;
        } else {
            sum -= moonCakes[i].stock;
            total += moonCakes[i].total_price;
        }
    }

    printf("%.2lf", total);

    return 0;
}
2020.2.29
这个题不难,很简单的贪心算法,但是有几点需要注意一下:
  • long long int N, sum; //这里如果是int就会错误
  • 结构体里的static比较算法写法问题。
发表于 2020-02-29 20:24:46 回复(0)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;
int n=0;           //月饼数量n
double need;	  //总需求need 

struct cake{
	double price;      //总售价 
	double num;        //库存 
}; 
cake arr[1000];

bool cmp(cake a,cake b){
	return (a.price/a.num)>(b.price/b.num);      //按单价从大到小排序 
}

int main(){
	scanf("%d%lf",&n,&need);
	for(int i=0;i<n;i++){    
		scanf("%lf",&arr[i].num);
	}
	for(int i=0;i<n;i++){
		scanf("%lf",&arr[i].price);
	}
	sort(arr,arr+n,cmp);
	
	double money=0;      //总收益 
	int i=0;
	while(need&&i<n){
		if(need>arr[i].num){
			need-=arr[i].num;
			money+=arr[i].price;
			i++; 
		}
		else {
			money+=need/arr[i].num*arr[i].price;
			break;
		}
	}
	printf("%.2f",money);
	
	
	return 0;
}


编辑于 2020-02-09 15:28:11 回复(0)
贪心算法 //每次选单价最高的那个
#include <bits/stdc++.h>
using namespace std;

struct node
{
	double price;//单价 
	double sum;//总库存 
	double Sprice;//总售价 
}r[1000]; 

bool cmp(node &a,node &b){return a.price>b.price;}

int main()
{
	int n;
	double d;
	while(cin>>n>>d)
	{
		double num=0;
		for(int i=0;i<n;i++)cin>>r[i].sum;
		for(int i=0;i<n;i++)cin>>r[i].Sprice;
		for(int i=0;i<n;i++)
		{
			r[i].price=r[i].Sprice/r[i].sum;
		}
		sort(r,r+n,cmp);
		for(int i=0;i<n&&d>0;i++)
		{
			if(r[i].sum>d)
			{
				num+=d*r[i].price;
				d-=d;
			}
			else 
			{
				num+=r[i].Sprice;
				d-=r[i].sum;
			}
		}
		printf("%.2lf\n",num);
	}
}

发表于 2020-02-08 12:49:58 回复(0)
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

/**
 * 
 * @author Sirice 贪心典型题
 */
public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int kind = sc.nextInt(); // kind <= 1000
		Cake[] arr = new Cake[kind];

		int d = sc.nextInt(); // capacity <= 500
		
		for (int i = 0; i < arr.length; i++) {
			arr[i] = new Cake();
			arr[i].setCap(sc.nextInt());
		}
		for (int i = 0; i < arr.length; i++) {
			arr[i].setPrice(sc.nextInt());
		}

		Arrays.sort(arr, new Comparator<Cake>() {
			@Override
			public int compare(Cake o1, Cake o2) {
				if ((float) o1.getPrice() / (float) o1.getCap() > (float) o2.getPrice() / (float) o2.getCap()) {
					return -1;
				} else {
					return 1;
				}
			}
		});
		
		float ans = 0;
		for(int i =0;i<arr.length;i++) {
			if(arr[i].getCap()<d) {
				d-=arr[i].getCap();
				ans+=(float)arr[i].getPrice();
			}else {
				ans+=(float)arr[i].getPrice()/(float)arr[i].getCap() * d;
				break;
			}
		}
		DecimalFormat df = new DecimalFormat(".00");
	    System.out.println(df.format(ans));

	}

}

class Cake {
	private int cap;
	private int price;

	public int getCap() {
		return cap;
	}

	public void setCap(int cap) {
		this.cap = cap;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public Cake(int cap, int price) {
		super();
		this.cap = cap;
		this.price = price;
	}

	public Cake() {
		super();
	}

}

发表于 2020-01-01 16:05:55 回复(0)
思路:
1.将输入的库存与总价分别保存
2.要求总价最高,必须求得均价
3.按均价从高到低排列
4.求总价,总需求高于该种月饼库存,直接把该种总价加上,不然就加上剩余需求*该种月饼单价
#include <iostream>
#include<iomanip>
using namespace std;
struct Sale
{
    int kucun;
    int sum;
    float ave;

}yuebing[1001];
int main()
{
    int N,M,i;
    int a,b;
    float Sum=0;
    cin>>N>>M;
    for(i=0;i<N;i++)
    {
        cin>>a;
        yuebing[i].kucun=a;
    }
    for(i=0;i<N;i++)
    {
        cin>>b;
        yuebing[i].sum=b;
        yuebing[i].ave=(float)b/(float)yuebing[i].kucun;
    }
    for(i=0;i<N;i++)//按均价从高到低排,方便后续计算
    {
        for(int j=i+1;j<N;j++)
        {
            if(yuebing[i].ave<yuebing[j].ave)
            {
                yuebing[1001]=yuebing[i];
                yuebing[i]=yuebing[j];
                yuebing[j]=yuebing[1001];
            }
        }
    }
    i=0;
    while(M>=yuebing[i].kucun)
    {
        Sum+=(float)yuebing[i].sum;
        M-=yuebing[i].kucun;
        i++;
    }
    Sum+=(float)M*yuebing[i].ave;
    cout<<fixed<<setprecision(2)<<Sum;
}

发表于 2019-07-11 09:04:28 回复(0)
#include<cstdio>
#include<algorithm>
using namespace std;
typedef struct mooncake
{
    double store;
    double sell;
    double price;
}mooncake;
mooncake cake[1010];
bool cmp(mooncake a,mooncake b)
{
    return a.price >b.price;
}
int main()
{
    int N;
    scanf("%d",&N);
    double need;
    scanf("%lf",&need);
    for(int i=0;i<N;++i)
    {
        scanf("%lf",&cake[i].store );
    }
    for(int j=0;j<N;++j)
    {
        scanf("%lf",&cake[j].sell );
        cake[j].price =cake[j].sell /cake[j].store ;
    }
    sort(&cake[0],&cake[N],cmp);
    double get=0;
    for(int i=0;i<N;++i)
    {
        if(cake[i].store <=need)
        {
            need=need-cake[i].store;
            get=get+cake[i].sell ;
        }
        else
        {
            get=get+cake[i].price *need;
            break;
        }
    
     printf("%.2f\n",get);
     return 0;
    
}
发表于 2019-04-16 20:13:36 回复(0)
#include<iostream>
using namespace std;
void maopao(double r[],int p[],int o[],int n)
{
 bool exchange=true;
 int i=1,j,temp2,temp3;double temp;
 while(exchange)
 {
  exchange=false;
  for(j=0;j<n-i;j++)
  {
   if(r[j]<r[j+1])
   {
    temp=r[j];temp2=p[j];temp3=o[j];
    r[j]=r[j+1];p[j]=p[j+1];o[j]=o[j+1];
    r[j+1]=temp;p[j+1]=temp2;o[j+1]=temp3;
    exchange=true;
   }
  }
  i++;
 }
}
int main()
{
 int i,N,need,j=1;
 int a[1001]={0},b[1001]={0};float ss=0,have=0;double c[1001]={0};//a[]存重量b[]存价格
 cin>>N>>need;//种类,需求
 for(i=0;i<N;i++)
 {
  cin>>a[i];//库存
 }
  for(i=0;i<N;i++)
 {
  
  cin>>b[i];//价格
 }
   for(i=0;i<N;i++)
 {
    if(a[i]!=0)
   c[i]=b[i]/(double)a[i];
    else c[i]=0;
 }
 maopao(c,a,b,N);
   i=0;
 while(have+a[i]<need)
 {
  have+=a[i]; ss+=b[i];
  i++;
 
 }
   while(j==1)
 {
  j--;
  ss+=b[i]*(need-have)/(double)a[i];
 }
 cout.precision(2);
 cout.setf(ios_base::fixed);
 cout<<ss;
  return 0;
}
错了,只能通过一个,哪儿错了,求解释
发表于 2019-03-20 18:25:58 回复(0)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define  ECS 1e-9
typedef struct
{
    int score;
    int score1;
    float score2;
}Elemtype;

typedef struct
{
    Elemtype *data;
}STACK;

void initializer_list(STACK *s,int n)
{
    s->data=(Elemtype *)malloc(sizeof(Elemtype)*n);
}

void input(STACK *s,int n,int m)
{
    int i,k,j;
    for(i=0;i<n;i++)
        scanf("%d",&s->data[i].score);
        for(i=0;i<n;i++)
            scanf("%d",&s->data[i].score1);
}

void  input1(STACK *s,int n,int m)
{
    int i,j,k;
        for(i=0;i<n;i++)
        {
             if(s->data[i].score!=ECS)
            s->data[i].score2=s->data[i].score1/((float)s->data[i].score);
        }
            for(i=0;i<n-1;i++)
            {
                k=i;
                for(j=i+1;j<n;j++)
                {
                    if(s->data[j].score2>s->data[k].score2)
                        k=j;
                }
                if(k!=i)
                {
                    s->data[n]=s->data[k];
                    s->data[k]=s->data[i];
                    s->data[i]=s->data[n];
    }
  }
}

float input2(STACK *s,int n,int m)
{
    int i;
    float sum=0;
            for(i=0;i<n;i++)
            {
                if(s->data[i].score<=m)
                    sum+=s->data[i].score1;
                    else
                    {
                        if(s->data[i].score/(float)m!=ECS)
                        sum+=s->data[i].score1/((s->data[i].score/(float)m));
                    }
                    m=m-s->data[i].score;
                    if(m<=0)break;
            }
            return sum;
}

int main()
{
    STACK s;
    Elemtype x;
    int m,n;
    float t;
    scanf("%d%d",&n,&m);
    initializer_list(&s,n);
    input(&s,n,m);
    input1(&s,n,m);
    t=input2(&s,n,m);
    printf("%.2f\n",t);
    return 0;
}
 
发表于 2018-05-11 21:28:44 回复(0)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
 
#define FOR(i,n) for(i = 0; i < n; i++)
 
using namespace std;
 
struct MoonCake
{
    doublestock;       //定义成int会有精度问题
    doubletotal_price;
    doubleprice;
}moonCake[1000];
intcmp(constvoid*, constvoid*);
 
intmain()
{
    intN, i;
    doubleD;
    doublesum = 0;
 
    cin.sync_with_stdio(false);
    cin >> N >> D;
    FOR(i, N)
    {
        //  scanf("%d", &moonCake[i].stock);
        cin >> moonCake[i].stock;
    }
    FOR(i, N)
    {
        //  scanf("%d", &moonCake[i].total_price);
        cin >> moonCake[i].total_price;
        moonCake[i].price = moonCake[i].total_price / moonCake[i].stock;
    }
 
    qsort(moonCake, N, sizeof(moonCake[0]), cmp);       //将月饼按单价从大到小排序
    i = 0;
    while(D > 0&& i < N)
    {
        if(moonCake[i].stock >= D)
        {
            sum += double(D) / moonCake[i].stock * moonCake[i].total_price;
            D = 0;
            break;
        }
        else
        {
            sum += moonCake[i].total_price;
            D -= moonCake[i].stock;
            i++;
        }
    }
    printf("%.2lf", sum);
    return0;
}
 
intcmp(constvoid*moonCakeA, constvoid*moonCakeB)
{
    return(*(MoonCake *)moonCakeA).price > (*(MoonCake *)moonCakeB).price ? -1: 1;
//很奇怪的事情,如果这行代码反过来写
//return(*(MoonCake *)moonCakeA).price < (*(MoonCake *)moonCakeB).price ? 1: -1;
//将会发生段溢出,这是为什么?
}
发表于 2018-04-21 19:23:29 回复(1)
#include<iostream>
#include<stdlib.h>
#include<algorithm>
#include<vector>
using namespace std;
struct M{
  double s;
  double p,price;

}m;
double cmp(M a,M b)
{
    return a.p>b.p;
}

int main()
{
//输出:总类数n,需求量num,库存量:s,售价:price
int n,num,s;
cin>>n>>num;
vector<M> stu(n);
for(int i=0;i<2*n;i++)
{   
     if(i<=n-1)
     {
         cin>>stu[i].s;
     }else{
     
      cin>>stu[i-n].price;
    
    
     
}
for(int i=0;i<n;i++)
     {
       stu[i].p=stu[i].price/stu[i].s;
        
     }
sort(stu.begin(),stu.end(),cmp);
int zl=0;
double price=0;
while(num>=0)
{
    if(num>stu[zl].s)
    {
        num=num-stu[zl].s;
        price+=stu[zl].price;
        zl++;
        continue;
    }
    if(num<=stu[zl].s)
    {
       price+=stu[zl].p*num;
        
       break;

     
    }
    
}
//cout<<price<<endl;
printf("%.2f",price);
system("pause");
return 0;
}
//敬请指教

发表于 2017-11-29 18:16:53 回复(0)
PAT能过,这里只能通过一个用例,不知道为什么?求告知
#include <bits/stdc++.h>
using namespace std;

struct sto {
double count;
double total;
double price;
};
bool comparison(sto a,sto b) {
return a.price>b.price;
}
int main() {
int i,j,N,D;
double pmax=0;
struct sto stock[1010];
struct sto temp;
scanf("%d%d",&N,&D);
for(i=0; i<N; i++) {
scanf("%lf",&stock[i].count);
}
for(i=0; i<N; i++) {
scanf("%lf",&stock[i].total);
stock[i].price=stock[i].total/stock[i].count;
}
sort(stock, stock+N, comparison);
for(i=0; i<N; i++) {
if(D-stock[i].count<0) {
pmax+=stock[i].price*D;
break;
} else {
//pmax+=stock[i].price*stock[i].count; 改成下面这样就对了。。。
pmax+=stock[i].total;
D=D-stock[i].count;
}
}
printf("%0.2lf",pmax);
return 0;
}
编辑于 2017-09-11 15:08:55 回复(2)
import java.util.*;
class pie implements Comparable<pie>{
public int l;//数量
public int m;//收益
public double y;
public int compareTo(pie p){
if(this.y>p.y) return 1;
else if(this.y<p.y) return -1;
else return 0;
}
}
public class Main {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int n=input.nextInt();
int d=input.nextInt();
pie[] p=new pie[n];
for(inti=0;i<n;i++){
p[i]=new pie();
p[i].l=input.nextInt();
}
for(inti=0;i<n;i++){
p[i].m=input.nextInt();
if(p[i].m==0) p[i].y=0;
elsep[i].y=(double)p[i].m/p[i].l;
}
java.util.Arrays.sort(p);
double max=0;
for(inti=n-1;i>=0;i--){
if(d<=p[i].l){
max=max+d*p[i].y;
break;
}
max=max+p[i].m;
d=d-p[i].l;
}
String r=String.format("%.2f", max);
System.out.println(r);
input.close();
}
}
编辑于 2017-08-02 10:40:34 回复(0)
import java.util.Arrays;
import java.util.Scanner;

public class _t394 {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while(in.hasNext()){
			int n=in.nextInt(),req=in.nextInt();
			int[] total=new int[n];
			double[] money=new double[n];
			double[] per=new double[n];
			for (int i = 0; i < n; i++) {
				total[i]=in.nextInt();
			}
			for (int i = 0; i < n; i++) {
				money[i]=in.nextDouble();
				per[i]=money[i]/total[i];
			}
			
			double temp=0;
			for (int i = n-1; i > 0; i--) {
				for (int j = 0; j < i; j++) {
					if(per[j]<per[j+1]){
						temp=per[j];
						per[j]=per[j+1];
						per[j+1]=temp;
						
						temp=total[j];
						total[j]=total[j+1];
						total[j+1]=(int) temp;
					}
				}
			}
//			System.out.println(Arrays.toString(per));
			
			int sum=0;
			double rs=0;
			for (int i = 0; i < n; i++) {
				sum+=total[i];
				if(req>sum){
					rs+=total[i]*per[i];
				}
				else{
					rs+=(req-sum+total[i])*per[i];
					break;
				}
			}
			System.out.printf("%.2f\n",rs);
		}
	}
}
没错呀,为什么只通过一个测试用例,求失败的用例~

发表于 2017-06-23 10:05:53 回复(21)
//大家好,这个可以算是贪心思想的应用;
#include <iostream>
#include <algorithm>
using namespace std;
typedef struct yuebin
{
	int dun;
	int price;
	double pre;
}yb;
static bool comp(yb a,yb b)
{
	return (a.pre>b.pre);
}
void helper(int count,int max)
{
	yb* arr=new yb[count];
	for(int i=0;i<count;i++)
	{
		cin>>arr[i].dun;
	}
	for(int i=0;i<count;i++)
	{
		cin>>arr[i].price;
	}
	for(int i=0;i<count;i++)
	{
		arr[i].pre=(double)arr[i].price/arr[i].dun;
	}
	//按照单价排序 
	sort(arr,arr+count,comp);
	int temp=max;
	double total=0.0;
	for(int i=0;i<count;i++)
	{
		if(temp==0) break;
		if(arr[i].dun>=temp && temp>0)
		{
			total+=(double)temp*arr[i].price/arr[i].dun;
			break;
		}
		else if(arr[i].dun<temp && temp>0)
		{
			temp = temp-arr[i].dun;
			total+=arr[i].price;
		}
	}
	printf("%.2f",total);
}
int main()
 {
 	int count,max;
 	while(cin>>count>>max)
 	{
 		helper(count,max);
	 }

    return 0;
}

发表于 2016-08-11 17:32:16 回复(0)
#include<iostream>
#include<algorithm>
using namespace std;
const int n = 1005;
struct m {
	double a, b, c;
	bool operator <(const m& l)const{
		return c > l.c;
	}
};
m A[n];
int main()
{
	int x, y, i = 0;
	double sum = 0;
	cin >> x >> y;
	for (int i = 0; i != x; i++){
		cin >> A[i].a;
	}
	for (int i = 0; i != x; i++){
		cin >> A[i].b;
		A[i].c = A[i].b / A[i].a;
	}
	sort(A, A + x);
	while (y){
		if (y >= A[i].a){
			sum += A[i].b;
			y -= A[i].a;
		}else{
			sum += A[i].c*y;
			y = 0;
		}
		i++;
	}
	printf("%.2lf", sum);
	return 0;
}
编辑于 2016-02-24 23:06:29 回复(0)
啥头像
总体思路:
  1. 建立一个月饼类,成员有库存,总售价,单价(密度)
  2. 利用set的自动排序功能
  3. 计算最大收益,根据需要量和已自动排序的set
代码如下:
#include <iostream>
#include <vector>
#include <set>
#include <iomanip>

using namespace std;

class MoonCake
{
public:
    int inventory;  // 库存
    int price;      // 总售价
    double density; // 单价
    MoonCake(int inventory, int price) {
        this->inventory = inventory;
        this->price = price;
        density = (double) price/inventory;
    }
    bool operator <(const MoonCake& another)const {
        return density < another.density;
    }
};

int main()
{
    // 读入数据
    int N, requirement;
    cin >> N >> requirement;
    vector<int> inventories(N, 0);
    multiset<MoonCake> moonCakes;
    for(int i=0; i<N; i++) {
        cin >> inventories[i];
    }
    for(int i=0; i<N; i++) {
        int tempPrice;
        cin >> tempPrice;
        MoonCake moonCake(inventories[i], tempPrice);
        moonCakes.insert(moonCake);
    }

    // 统计最大收益
    double earnings = 0.0; int sum =0;
    multiset<MoonCake>::reverse_iterator iter;
    for(iter=moonCakes.rbegin(); iter!=moonCakes.rend(); iter++) {
        int currentInventory = (*iter).inventory;
        if(currentInventory < (requirement-sum)) {
            sum += currentInventory;
            earnings += (*iter).price;
        } else {
            earnings += (requirement-sum)*((*iter).density);
            break;
        }
    }
    cout << fixed << setprecision(2) << earnings << endl;
    return 0;
} 


编辑于 2015-10-15 19:40:57 回复(0)
N, D = [int(i) for i in input().split()]
storage = [float(i) for i in input().split()]
price = [float(i) for i in input().split()]

def cacu_profit():
    data = list(map(lambda x: (x[1] / x[0], x[0]), filter(lambda x: x[0] != 0, zip(storage, price))))
    data.sort(key=lambda x: x[0], reverse=True)
    profit, weight= 0, 0
    while weight < D:
        temp = data.pop(0)
        if temp[1] + weight <= D:
            weight += temp[1]
            profit += temp[0] * temp[1]
        else:
            profit += temp[0] * (D - weight)
            weight = D
        if data == []:
            break
    print('%.2f' % profit)
    
cacu_profit()
我不知道是哪个地方错了,感觉逻辑上是没问题的,真心求大神们帮我查查错,谢谢!!!!
编辑于 2019-05-23 22:07:02 回复(0)
#include <algorithm>
#include <iostream>
using namespace std;
struct mc{
    double quantity,pay,spay;     
}s[1010];
bool cmp(mc a,mc b){
    return a.spay>b.spay;
}
int main(){
    int x,y,i;double max=0;
    cin>>x>>y;
    for(i=0;i<x;i++)
        cin>>s[i].quantity;
    for(i=0;i<x;i++){
        cin>>s[i].pay;
        s[i].spay=s[i].pay/s[i].quantity;
    }
    sort(s,s+x,cmp);       //按单价从高到低排序
    for(i=0;i<x;i++){
        if(s[i].quantity<=y){   //需求量高于库存量,卖出第i种所有月饼
            y-=s[i].quantity;
            max+=s[i].pay;
        }
        else{
            max+=y*s[i].spay;   //库存量高于需求量,卖出剩余需求量的月饼
            break;
        }
    }
    printf("%.2f",max);

    return 0;
}
发表于 2018-01-26 20:43:40 回复(0)