首页 > 专题系列 > Java解POJ > POJ 2501 Average Speed [解题报告] Java
2013
11-11

POJ 2501 Average Speed [解题报告] Java

Average Speed

问题描述 :

You have bought a car in order to drive from Waterloo to a big city. The odometer on their car is broken, so you cannot measure distance. But the speedometer and cruise control both work, so the car can maintain a constant speed which can be adjusted from time to time in response to speed limits, traffic jams, and border queues. You have a stopwatch and note the elapsed time every time the speed changes. From time to time you wonder, “how far have I come?”. To solve this problem you must write a program to run on your laptop computer in the passenger seat.

输入:

Standard input contains several lines of input: Each speed change is indicated by a line specifying the elapsed time since the beginning of the trip (hh:mm:ss), followed by the new speed in km/h. Each query is indicated by a line containing the elapsed time. At the outset of the trip the car is stationary. Elapsed times are given in non-decreasing order and there is at most one speed change at any given time.

输出:

For each query in standard input, you should print a line giving the time and the distance travelled, in the format below.

样例输入:

00:00:01 100
00:15:01
00:30:01
01:00:01 50
03:00:01
03:00:05 140

样例输出:

00:15:01 25.00 km
00:30:01 50.00 km
03:00:01 200.00 km

解题代码:

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

public class Main 
{ 
    static int hh; 
    static int mm; 
    static int ss; 
    static double sumDist; 
    static double speed; 
    public static void main(String[] args) throws Exception 
    { 
        readFile(); 
    } 

    static void readFile() throws Exception 
    { 
        BufferedReader br = new BufferedReader( 
            //new FileReader("in.in")); 
            new InputStreamReader(System.in)); 
        StringTokenizer st = null; 
        String stemp = null; 
        while((stemp=br.readLine())!=null) 
        { 
            st = new StringTokenizer(stemp," "); 
            if(st.countTokens()==2) 
            { 
                stemp = st.nextToken(); 
                double tempspeed = Double.valueOf(st.nextToken()) / 3600; 
                st = new StringTokenizer(stemp,":"); 
                process(Integer.valueOf(st.nextToken()), 
                    Integer.valueOf(st.nextToken()), 
                    Integer.valueOf(st.nextToken()),-1); 
                speed = tempspeed; 
            } 
            else 
            { 
                st = new StringTokenizer(st.nextToken(),":"); 
                process(Integer.valueOf(st.nextToken()), 
                    Integer.valueOf(st.nextToken()), 
                    Integer.valueOf(st.nextToken()),1); 
            } 
        } 
    } 
    static int fff = 0; 
     
    static void process(int h,int m,int s,int flag) 
    { 
        int sumSecond = 0; 
        sumSecond += (s-ss); 
        sumSecond += ((m-mm)*60); 
        sumSecond += ((h-hh)*3600); 
        sumDist += (sumSecond * speed); 
        if(flag==1) 
        { 
            if(fff!=0) 
                System.out.println(); 
            fff++; 
            display(h,m,s); 
            System.out.print(saveDigit(sumDist,2)); 
            System.out.print(" km"); 
        } 
        hh = h; 
        mm = m; 
        ss = s; 
    } 
     
    static String saveDigit(double d,int n) 
    { 
        StringBuffer sb = new StringBuffer("###."); 
        for(int i=0; i< n; i++) 
            sb.append("0"); 
        DecimalFormat df2  = new DecimalFormat(sb.toString()); 
        StringBuffer result = new StringBuffer(df2.format(d)); 
        if(result.indexOf(".")==0) 
            result.insert(0,"0"); 
        return result.toString(); 
    } 
     
    static void display(int h,int m,int s) 
    { 
        if(h< 10) 
            System.out.print("0"); 
        System.out.print(h+":"); 
        if(m< 10) 
            System.out.print("0"); 
        System.out.print(m+":"); 
        if(s< 10) 
            System.out.print("0"); 
        System.out.print(s+" "); 
    } 
}

  1. 我还有个问题想请教一下,就是感觉对于新手来说,递归理解起来有些困难,不知有没有什么好的方法或者什么好的建议?

  2. #!/usr/bin/env python
    def cou(n):
    arr =
    i = 1
    while(i<n):
    arr.append(arr[i-1]+selfcount(i))
    i+=1
    return arr[n-1]

    def selfcount(n):
    count = 0
    while(n):
    if n%10 == 1:
    count += 1
    n /= 10
    return count