首页 > 专题系列 > Java解POJ > POJ 2555 Drink, on Ice [解题报告] Java
2013
11-11

POJ 2555 Drink, on Ice [解题报告] Java

Drink, on Ice

问题描述 :

A good drink is always served on ice. That said, the amount of ice is what makes the difference. If it is too much, the drink will be well cooled, however, this is a bit of fraud as there could be less ice (and more Vodka for example). On the other hand, if there is too little ice the drink is warm which is unacceptable. You are to help the bartender, of course neither with mixing nor drinking, but with calculating the expected outcome of such mixtures.

To make things easier, we assume that pure water is mixed with ice in a closed system, i.e., there is no problem with the outside temperature or the warming of the bottle, etc. Therefore, after a some time has passed, the system may be regarded as balanced (there is no further change in temperature and no more melting or freezing). Your job is to calculate the final temperature of this balanced system and the amount of ice and water in this equilibrium state.

As you know from physics, it takes 4.19 Joule to heat one gram of water one Kelvin, whereas it takes 2.09 Joule if it is ice. We define the capacities cw = 4.19 J/(g*K) and ci = 2.09 J/(g*K). Melting one gram of ice takes 335 Joule, where the temperature remains constant at zero. We define the constant em = 335 J/g. The total thermal energy of the ice and the water before the experiment is equal to the thermal energy of the final mixture.

The figure below shows the energy of one gram of ice, ice-water-mixture, or water, where the temperature is measured relative to -30 degrees Celsius. The jump at 0 degrees represents the melting of ice to water. The amount of energy gained is proportional to the amount of ice already melted.

输入:

The input contains several test cases. Each test case consists of four real numbers mw, mi, tw, ti. The mass of water mw and the mass of ice mi are both non-negative, given in grams, and mw + mi > 0. The water temperature tw and the ice temperature ti follow, both given in degrees Celsius, and you may assume that -30 < ti <= 0 <= tw < 100. The last test case is followed by four zeroes.

输出:

For each test case output the amount of ice and water in grams and the final temperature of the mixture in degrees Celsius. All numbers must be rounded to one digit. Adhere to the sample output for the exact format to use.

样例输入:

100 20 50 -10
100 22 0  0
100 35 25 -10.5
10  90 25 -28
0 0 0 0

样例输出:

0.0 g of ice and 120.0 g of water at 27.5 C
22.0 g of ice and 100.0 g of water at 0.0 C
6.0 g of ice and 129.0 g of water at 0.0 C
100.0 g of ice and 0.0 g of water at -4.2 C

解题代码:

/* @author:zeropinzuo */
import java.io.*;
import java.util.*;
import java.text.DecimalFormat;

public class Main{
 static Scanner cin;

 static double ci = 2.09, cw = 4.19;
 static double em = 335; 

 public static void main(String args[]){
	cin = new Scanner(System.in);
	while(run())
			;
 }

 static boolean run(){
  double mw = cin.nextDouble();
  double mi = cin.nextDouble();
  double tw = cin.nextDouble();
  double ti = cin.nextDouble();

  if((mw+mi)==0)
	return false;

  DecimalFormat df = new DecimalFormat("#0.0");
		
  double Energy = waterEnergy(mw, tw)+iceEnergy(mi, ti);
  double averE = Energy/(mw+mi);

  double iceThreshold = ci*30;
  double waterThreshold = iceThreshold+em;

  if(averE <= iceThreshold){
    double temp = -30+averE/ci;
    System.out.println(df.format(mw+mi)+" g of ice and 0.0 g of water at "+df.format(temp)+" C");
  }
  else if(averE < waterThreshold){
   double fmw = (mw+mi)*(averE-iceThreshold)/em;
   double fmi = mw+mi-fmw;
   System.out.println(df.format(fmi)+" g of ice and "+df.format(fmw)+" g of water at 0.0 C");
  }
  else{
   double temp = (averE-waterThreshold)/cw;
   System.out.println("0.0 g of ice and "+df.format(mw+mi)+" g of water at "+df.format(temp)+" C");
  }

  return true;
		
}

static double waterEnergy(double mw, double tw){
  double waterThreshold = ci*30+em;
  return mw*(waterThreshold+tw*cw);
 }

static double iceEnergy(double mi, double ti){
	return mi*(ti+30)*ci;
 }
}

  1. 第二种想法,我想来好久,为啥需要一个newhead,发现是把最后一个节点一直返回到嘴上面这层函数。厉害,这道题之前没样子想过。