PAT B1020 moon cake solving record

Very simple greedy algorithm, but a start to do when there are still a lot without taking into account the
idea is very simple, read the input and then calculates a variety of moon cake price, according to unit price of moon cake sort of moon cake high unit price preference.
Here to talk about a few errors committed by a start. (I used to write java)

1: In the beginning submit, all the answers wrong, so check the topic and found that requires two decimal places. So use DecimalFormat tools, two decimal places, then give it a try.

DecimalFormat df = new DecimalFormat("#.00"); 
String result = df.format(amount);

2: submit once again, a few more correct answer, but the answer is still wrong, thought for a moment, if the moon cake needs is 0, the output should be "0.00", but my program output of the ".00"

3: Submit again, most of them right, but there is still a wrong answer, like a long time to no avail, Internet search a bit later, after friends suggested that need to be considered in short supply situation, namely the total amount of moon cake is less than demand, which all should return the total price of moon cake. So I wrote a case of test cases, test, find obviously a runtime error. . .
Okay, then change.
4: finally all the correct
feelings: On leetcode accustomed to doing, to see the wrong test results can control the purposeful, a bit less in the PAT aiming to adapt.
I attach the following code

import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.io.BufferedReader;
import java.io.IOException;

public class Main
{
	public static BufferedReader br = 
			new BufferedReader(new InputStreamReader(System.in)); 
	public static void main(String[] args) throws IOException
	{
		String[] info = br.readLine().split(" ");
		int kind = Integer.valueOf(info[0]);
		double request = Integer.valueOf(info[1]);
		String[] stockArr = br.readLine().split(" ");
		String[] priceArr = br.readLine().split(" ");
		MoonCake[] mc = new MoonCake[kind];
		for(int i = 0; i < kind; i++)
		{
			double s = Double.valueOf(stockArr[i]);
			double p = Double.valueOf(priceArr[i])/s;
			MoonCake m = new MoonCake(s, p);
			mc[i] = m;
		}
		double count = 0;
		int index = 0;
		Arrays.sort(mc);
		while(request > 0)
		{
			if(mc[index].stock >= request )
			{
				count += mc[index].price * request;
				request = 0;
			}else 
			{
				request -= mc[index].stock;
				count += mc[index].price * mc[index].stock;
				index+=1;
			}
		}
		DecimalFormat df = new DecimalFormat("#.00"); 
		String result = df.format(count);
		if(result.equals(".00"))
		{
			System.out.println("0.00");
		}
		else {
			System.out.println(result);
		}		
	}
}
class MoonCake implements Comparable<MoonCake>
{
    public double stock;
    public double price;   
    public MoonCake(double stock, double price) {
		// TODO Auto-generated constructor stub
    	
    	this.stock = stock;
    	this.price = price;
	}
	@Override
	public int compareTo(MoonCake o) {
		double gap = o.price - this.price;
		if(gap > 0)
		{
			return 1;
		}else if(gap < 0){
			return -1;
		}else {
			return 0;
		}
	}
}
Released six original articles · won praise 0 · Views 98

Guess you like

Origin blog.csdn.net/weixin_41276957/article/details/104703935