PKU_ACM_2501_Average Speed

 2501_Average Speed

 

原题连接

 

简单题

 

  1. import java.io.*;
  2. import java.util.*;
  3. import java.text.DecimalFormat;
  4. public class Main
  5. {
  6.     static int hh;
  7.     static int mm;
  8.     static int ss;
  9.     static double sumDist;
  10.     static double speed;
  11.     public static void main(String[] args) throws Exception
  12.     {
  13.         readFile();
  14.     }
  15.     static void readFile() throws Exception
  16.     {
  17.         BufferedReader br = new BufferedReader(
  18.             //new FileReader("in.in"));
  19.             new InputStreamReader(System.in));
  20.         StringTokenizer st = null;
  21.         String stemp = null;
  22.         while((stemp=br.readLine())!=null)
  23.         {
  24.             st = new StringTokenizer(stemp," ");
  25.             if(st.countTokens()==2)
  26.             {
  27.                 stemp = st.nextToken();
  28.                 double tempspeed = Double.valueOf(st.nextToken()) / 3600;
  29.                 st = new StringTokenizer(stemp,":");
  30.                 process(Integer.valueOf(st.nextToken()),
  31.                     Integer.valueOf(st.nextToken()),
  32.                     Integer.valueOf(st.nextToken()),-1);
  33.                 speed = tempspeed;
  34.             }
  35.             else
  36.             {
  37.                 st = new StringTokenizer(st.nextToken(),":");
  38.                 process(Integer.valueOf(st.nextToken()),
  39.                     Integer.valueOf(st.nextToken()),
  40.                     Integer.valueOf(st.nextToken()),1);
  41.             }
  42.         }
  43.     }
  44.     static int fff = 0;
  45.     
  46.     static void process(int h,int m,int s,int flag)
  47.     {
  48.         int sumSecond = 0;
  49.         sumSecond += (s-ss);
  50.         sumSecond += ((m-mm)*60);
  51.         sumSecond += ((h-hh)*3600);
  52.         sumDist += (sumSecond * speed);
  53.         if(flag==1)
  54.         {
  55.             if(fff!=0)
  56.                 System.out.println();
  57.             fff++;
  58.             display(h,m,s);
  59.             System.out.print(saveDigit(sumDist,2));
  60.             System.out.print(" km");
  61.         }
  62.         hh = h;
  63.         mm = m;
  64.         ss = s;
  65.     }
  66.     
  67.     static String saveDigit(double d,int n)
  68.     {
  69.         StringBuffer sb = new StringBuffer("###.");
  70.         for(int i=0; i<n; i++)
  71.             sb.append("0");
  72.         DecimalFormat df2  = new DecimalFormat(sb.toString());
  73.         StringBuffer result = new StringBuffer(df2.format(d));
  74.         if(result.indexOf(".")==0)
  75.             result.insert(0,"0");
  76.         return result.toString();
  77.     }
  78.     
  79.     static void display(int h,int m,int s)
  80.     {
  81.         if(h<10)
  82.             System.out.print("0");
  83.         System.out.print(h+":");
  84.         if(m<10)
  85.             System.out.print("0");
  86.         System.out.print(m+":");
  87.         if(s<10)
  88.             System.out.print("0");
  89.         System.out.print(s+" ");
  90.     }
  91. }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值