首页 > 专题系列 > Java解POJ > POJ 2578 Keep on Truckin’ [解题报告] Java
2013
11-11

POJ 2578 Keep on Truckin’ [解题报告] Java

Keep on Truckin’

问题描述 :

Boudreaux and Thibodeaux are on the road again . . .

“Boudreaux, we have to get this shipment of mudbugs to Baton Rouge by tonight!”

“Don’t worry, Thibodeaux, I already checked ahead. There are three underpasses and our 18-wheeler will fit through all of them, so just keep that motor running!”

“We’re not going to make it, I say!”

So, which is it: will there be a very messy accident on Interstate 10, or is Thibodeaux just letting the sound of his own wheels drive him crazy?

输入:

Input to this problem will consist of a single data set. The data set will be formatted according to the following description.

The data set will consist of a single line containing 3 numbers, separated by single spaces. Each number represents the height of a single underpass in inches. Each number will be between 0 and 300 inclusive.

输出:

There will be exactly one line of output. This line will be:

NO CRASH

if the height of the 18-wheeler is less than the height of each of the underpasses, or:

CRASH X

otherwise, where X is the height of the first underpass in the data set that the 18-wheeler is unable to go under (which means its height is less than or equal to the height of the 18-wheeler).

The height of the 18-wheeler is 168 inches.

样例输入:

180 160 170

样例输出:

CRASH 160

解题代码:

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


public class Main {

/**
 * @param args
 */
public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner in  = new Scanner(System.in);
	int height = 168;
	int crash = 0;
	for(int i = 0; i< 3; i++){
		int temp = in.nextInt();
		if(temp<=height){
		  System.out.println("CRASH "+temp);
		  crash=1;
		  break;
		}
	}
	if(crash==0)
		System.out.println("NO CRASH");
 }

}

  1. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。