[JAVA][HDU 1861][九度 1022][游船出租]

本文介绍了一个Java程序,该程序能够处理输入的船只租赁数据,包括船只编号、操作类型(开始租赁'S'或结束租赁'E')以及时间。通过解析这些数据,程序可以计算出租赁船只的数量以及平均租赁时间。
[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. import java.io.BufferedInputStream;  
  2. import java.util.Scanner;  
  3. import java.lang.String;  
  4.   
  5. public class Main {  
  6.     public static int time(String a, String b) {// got the D-value of two times  
  7.         String tmpa[] = a.split(":");  
  8.         String tmpb[] = b.split(":");  
  9.         int h1 = Integer.valueOf(tmpa[0]);  
  10.         int m1 = Integer.valueOf(tmpa[1]);  
  11.         int h2 = Integer.valueOf(tmpb[0]);  
  12.         int m2 = Integer.valueOf(tmpb[1]);  
  13.   
  14.         return ((h2 - h1) * 60 + m2 - m1);  
  15.     }  
  16.   
  17.     public static void main(String[] args) {  
  18.         int numOfCases = 0;// record the num of cases  
  19.         int boatNo[] = new int[101];// record the num of boats  
  20.         char opOfBoat[] = new char[101];// record the opeator sign of each boats  
  21.         String time[] = new String[101];  
  22.         Scanner sc = new Scanner(new BufferedInputStream(System.in));  
  23.   
  24.         int chkflag;  
  25.         chkflag = sc.nextInt();// if input '-1' then exit  
  26.   
  27.         while (chkflag != -1) {  
  28.             boatNo[numOfCases] = chkflag;  
  29.             opOfBoat[numOfCases] = sc.next().charAt(0);  
  30.             time[numOfCases] = sc.next();  
  31.   
  32.             // if input '0' means is the end of one testcase  
  33.             if (boatNo[numOfCases] != 0) {  
  34.                 numOfCases++;  
  35.             } else {// check all data  
  36.                 if (numOfCases == 0) {  
  37.                     System.out.println("0 0");  
  38.                 } else {  
  39.                     int sumRented = 0// record the sum num of the boats rented and backed  
  40.                     int sumTime = 0;  
  41.                     boolean isDataUsed[] = new boolean[numOfCases];  
  42.                     for (int i = 0; i < numOfCases; i++) {  
  43.                         if (isDataUsed[i] || opOfBoat[i] != 'S') {  
  44.                             // if we have check this data of this is not the data of boat renting,ingore it  
  45.                             continue;  
  46.                         } else {  
  47.                             for (int j = i + 1; j < numOfCases; j++) {  
  48.                                 if (boatNo[j] == boatNo[i] && opOfBoat[j] == 'E') {  
  49.                                     // if we found the first data of the same boat returning then check the time  
  50.                                     sumTime += time(time[i], time[j]);  
  51.                                     sumRented++;  
  52.                                     isDataUsed[i] = true;  
  53.                                     isDataUsed[j] = true;  
  54.                                     break;  
  55.                                 }  
  56.                             }  
  57.                         }  
  58.                     }  
  59.                     System.out.println(sumRented + " " + (int) Math.rint((double) sumTime / (double) sumRented));  
  60.                 }  
  61.                 numOfCases = 0;// reset var  
  62.             }  
  63.   
  64.             chkflag = sc.nextInt();  
  65.         }  
  66.     }  
  67. }  


一次AC的水题,纯粹记录一下代码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值