首页 > 试题广场 >

月饼 (25)

[编程题]月饼 (25)
  • 热度指数:34728 时间限制: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
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int a = scan.nextInt();
        int sum = scan.nextInt();
        double[] b = new double[a * 2];
        double[] c = new double[a];
        for (int i = 0; i < a * 2; i++) {
            b[i] = scan.nextInt();
        }
        scan.close();
        for (int i = 0; i < c.length; i++) {
            c[i] = (b[i + c.length] / b[i]);
        }
        change(b, c);
        double maxNumber = 0;
        double maxMoney = 0;
        int i;
        for (i = 0; i < c.length; i++) {
            if(sum > maxNumber) {
                maxNumber += b[i];
                maxMoney += b[i + a];
            }else {
                maxMoney -= (maxNumber - sum) * c[i - 1];
                break;
            }
        }
        System.out.println(String.format("%.2f", maxMoney));
    }

    private static void change(double[] b, double[] c) {
        for (int i = 0; i < c.length - 1; i++) {
            for (int j = 0; j < c.length - i - 1; j++) {
                if(c[j + 1] > c[j]) {
                    double t = b[j];
                    b[j] = b[j + 1];
                    b[j + 1] = t;
                    t = b[j + c.length];
                    b[j + c.length] = b[j + c.length + 1];
                    b[j + c.length + 1] = t;
                    t = c[j];
                    c[j] = c[j + 1];
                    c[j + 1] = t;
                }
            }
        }
    }
}

发表于 2019-05-30 18:08:35 回复(0)
import java.util.Scanner;
public class Main{
 public static int max(double []p) {
  int k=0;
  double max=0;
  for(int i=0;i<p.length;i++) {
   if(p[i]>max) {
    k=i;
    max=p[i];
   }
  }
  return k;
 }
 public static void main(String[] args) {
  Scanner in=new Scanner(System.in);
  String []nn=in.nextLine().split(" ");
  int a=Integer.parseInt(nn[0]);
  int b=Integer.parseInt(nn[1]);
  double []l1=new double[a];
  double []l2=new double[a];
  double []l3=new double[a];
  String []p=in.nextLine().split(" ");
  String []o=in.nextLine().split(" ");
  for(int i=0;i<a;i++) {
   l1[i]=Double.parseDouble(p[i]);
   l2[i]=Double.parseDouble(o[i]);
   l3[i]=l2[i]/l1[i];
  }
  double sum=0;
  while(b>0) {
   if(b>l1[max(l3)]) {
    sum+=l2[max(l3)];
    b-=l1[max(l3)];
    l3[max(l3)]=0;
   }
   else {
    sum+=(l3[max(l3)]*b);
    break;
   }
  }
  System.out.printf("%.2f",sum);
 }
}

发表于 2019-03-12 11:58:28 回复(0)
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.text.DecimalFormat;
public class Mooncake {
       public static void main (String args []){
        List<Student> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int max = sc.nextInt();
        for(int i=0;i<n;i++){
            list.add(new Student(sc.nextInt(),0));
        }
        for(int i=0;i<n;i++){
            list.get(i).setSale(sc.nextDouble());
            list.get(i).setPrice();
        }
        Collections.sort(list);
        Do(list,max);
    }    
    public static void Do(List<Student> list,int max){
        double sum = 0;
        DecimalFormat d = new DecimalFormat("#.00");
        for (int i=0;i<list.size()&&max>0;i++ ){
            if (max<list.get(i).stocks){                
                sum += (max*list.get(i).price); 
                System.out.println(d.format(sum));
                max=0;
                continue;
            }
            else{
                max -= list.get(i).stocks;
                sum += list.get(i).sale;; 
            }       
        }
    }
}
class Student implements Comparable<Student>{
    int stocks;
    double sale;
    double price;
    Student(int stocks,double sale){
        this.stocks = stocks;
        this.sale = sale;
        this.price = sale/stocks;
    }
    public int compareTo(Student stu){
        if (this.price>stu.price){
            return -1;
        }
        else if (this.price>stu.price){
            return 1;
        }
        else{
            return 0;
        }
    }
    public void setSale(double sale){
        this.sale = sale;
    }
    public void setPrice(){
        this.price=this.sale/this.stocks;
    }
}
发表于 2017-12-04 17:18:19 回复(0)
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.Iterator;
import java.util.Scanner;
import java.util.TreeSet;

public class Main
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int D = sc.nextInt();
		int[] amount = new int[N];
		int[] money = new int[N];
		MoonCake[] moon = new MoonCake[N];
		TreeSet<MoonCake> ts = new TreeSet<MoonCake>();
		
		for(int x = 0; x < N; x++)
		{
			amount[x] = sc.nextInt();
		}
		for(int x = 0; x < N; x++)
		{
			money[x] = sc.nextInt();
			moon[x] = new MoonCake(amount[x], money[x]);
			ts.add(moon[x]);
		}
		sc.close();
		
		int amountSum = 0;
		double moneySum = 0;
		Iterator<MoonCake> it = ts.iterator();
		while(it.hasNext())
		{
			MoonCake m = it.next();
			int tempAmount = amountSum + m.getAmount();
			if(tempAmount > D)
			{
				double tempMoney = (tempAmount - D) * (m.getMoney() * 1.0 / m.getAmount());
				moneySum = moneySum + m.getMoney() - tempMoney;
				break;
			}
			else
			{
				amountSum += m.getAmount();
				moneySum += m.getMoney();
			}
		}
		System.out.printf("%.2f",moneySum);
	}
}

class MoonCake implements Comparable<MoonCake>
{
	private int amount;
	private int money;
	
	MoonCake(int amount, int money)
	{
		this.amount = amount;
		this.money = money;
	}
	
	public int getAmount()
	{
		return this.amount;
	}
	
	public int getMoney()
	{
		return money;
	}

	public int compareTo(MoonCake o)
	{
		double thisPrice = this.money * 1.0 / this.amount;
		double oPrice = o.money * 1.0 / o.amount;
		double num = oPrice - thisPrice;
		if(num >= -0.000001 && num <= 0.000001)
			return o.money - this.money;
		return num > 0 ? 1 : -1;
	}
}
将moon数组按月饼的单价排序,然后将前面D吨的价钱相加即可获得结果

发表于 2016-06-23 22:25:12 回复(0)