题目1022:游船出租

来源:互联网 发布:c语言分配内存 编辑:程序博客网 时间:2024/04/28 18:35

题目1022:游船出租

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:3264

解决:1289

题目描述:
    现有公园游船租赁处请你编写一个租船管理系统。当游客租船时,管理员输入船号并按下S键,系统开始计时;当游客还船时,管理员输入船号并按下E键,系统结束计时。船号为不超过100的正整数。当管理员将0作为船号输入时,表示一天租船工作结束,系统应输出当天的游客租船次数和平均租船时间。
    注意:由于线路偶尔会有故障,可能出现不完整的纪录,即只有租船没有还船,或者只有还船没有租船的纪录,系统应能自动忽略这种无效纪录。
输入:

    测试输入包含若干测试用例,每个测试用例为一整天的租船纪录,格式为:
    船号(1~100) 键值(S或E) 发生时间(小时:分钟)
    每一天的纪录保证按时间递增的顺序给出。当读到船号为-1时,全部输入结束,相应的结果不要输出。

输出:
    对每个测试用例输出1行,即当天的游客租船次数和平均租船时间(以分钟为单位的精确到个位的整数时间)。
样例输入:
1 S 08:102 S 08:351 E 10:002 E 13:160 S 17:000 S 17:003 E 08:101 S 08:202 S 09:001 E 09:200 E 17:00-1
样例输出:
2 1960 01 60
参考代码:

import java.util.ArrayList;import java.util.List;import java.util.Scanner;public class Main {private  static class Boat{int Stime=9999;int Etime=9999;int No;public Boat(int No,int Stime,int Etime ) {this.setEtime(Etime);this.setStime(Stime);this.setNo(No);}public void setStime(int S_time){this.Stime = S_time;}public void setEtime(int E_time){this.Etime = E_time;}public void setNo(int No){this.No = No;}public int getStime(){return this.Stime;}public int getEime(){return this.Etime;}public int getNo(){return this.No;}public int gettime(){return this.Etime-this.Stime;}}public static void main(String arg[]){Scanner sc = new Scanner(System.in);while(sc.hasNext()){List<Boat> boats = new ArrayList<Boat>();String temp = sc.nextLine();if(temp.equals("-1")) break;while(true){//分割字符串String[] str = temp.split(" ");Integer No = Integer.parseInt(str[0]);if(No==0) break;//转化成分钟计的时间函数String[] temp1 = str[2].split(":");Integer time = Integer.parseInt(temp1[0])*60+Integer.parseInt(temp1[1]); Integer flag=0;//是否已经在列表中for(Boat i:boats){if(i.getNo()==No) {flag=1;if(str[1].equals("S")) i.setStime(time);if(str[1].equals("E")) i.setEtime(time);break;}}//不在列表中if(flag==0) {if(str[1].equals("S")) boats.add(new Boat(No, time, 9999));if(str[1].equals("E")) boats.add(new Boat(No, 9999, time));}temp = sc.nextLine();}//输出Integer totaltime=0;Integer count=0;for(Boat i : boats){if(i.getEime()==9999 || i.getStime()==9999) continue;totaltime += i.gettime();count++;}System.out.println(count==0?count+" "+0:count+" "+(Integer)Math.round((float)totaltime/(float)count));boats.clear();}}}




0 0
原创粉丝点击