首页 > 专题系列 > Java解POJ > POJ 2871 A Simple Question of Chemistry [解题报告] Java
2013
11-12

POJ 2871 A Simple Question of Chemistry [解题报告] Java

A Simple Question of Chemistry

问题描述 :

Your chemistry lab instructor is a very enthusiastic graduate student who clearly has forgotten what their undergraduate Chemistry 101 lab experience was like. Your instructor has come up with the brilliant idea that you will monitor the temperature of your mixture every minute for the entire lab. You will then plot the rate of change for the entire duration of the lab.

Being a promising computer scientist, you know you can automate part of this procedure, so you are writing a program you can run on your laptop during chemistry labs. (Laptops are only occasionally dissolved by the chemicals used in such labs.) You will write a program that will let you enter in each temperature as you observe it. The program will then calculate the difference between this temperature and the previous one, and print out the difference. Then you can feed this input into a simple graphing program and finish your plot before you leave the chemistry lab.

输入:

The input is a series of temperatures, one per line, ranging from -10 to 200. The temperatures may be specified up to two decimal places. After the final observation, the number 999 will indicate the end of the input data stream. All data sets will have at least two temperature observations.

输出:

Your program should output a series of differences between each temperature and the previous temperature. There is one fewer difference observed than the number of temperature observations (output nothing for the first temperature). Differences are always output to two decimal points, with no leading zeroes (except for the ones place for a number less than 1, such as 0.01) or spaces. After the final output, print a line with “End of Output”.

样例输入:

10.0
12.05
30.25
20
999

样例输出:

2.05
18.20
-10.25
End of Output

解题代码:

//* @author 洪晓鹏<[email protected]>
import java.util.Scanner;


public class Main {
 public static void main(String[] args)
 {
  Scanner in = new Scanner(System.in);
  double first = in.nextDouble();
  while(true)
   {
	double second = in.nextDouble();
	if(second == 999)
	{
		System.out.println("End of Output");
		break;
	}
	System.out.printf("%2.2f",second - first);
	System.out.println();
	first = second;
    }
  }
}

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

  2. 站长,你好!
    你创办的的网站非常好,为我们学习算法练习编程提供了一个很好的平台,我想给你提个小建议,就是要能把每道题目的难度标出来就好了,这样我们学习起来会有一个循序渐进的过程!