题目1022:游船出租
参考代码:
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();
}
}
}