由於Stack是FILO的性質拿13hour 會變成31 ruoh所以我使用了FIFO性質的Queue進行處理 下面是代碼
package b981;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class b981 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
long ans = 0;
String str = scanner.nextLine();
Queue<String> ints = new LinkedList<>();
Queue<String> time = new LinkedList<>();
for(char c: str.toCharArray()) {
if ("houirmsn".contains(String.valueOf(c))) {
time.add(String.valueOf(c));
}else {
String opString = "";
String intString = "";
while (time.peek() !=null) {
opString+=time.poll();
}
if(!opString.equals("")) {
while (ints.peek()!=null) {
intString += ints.poll();
}
ans += eval(Float.valueOf(intString), opString);
}
ints.add(String.valueOf(c));
}
}
String opString = "";
String intString = "";
while (time.peek() !=null) {
opString+=time.poll();
}
if(!opString.equals("")) {
while (ints.peek()!=null) {
intString += ints.poll();
}
ans += eval(Float.valueOf(intString), opString);
}
System.out.println(ans);
}
}
public static long eval(Float a,String op) {
switch (op) {
case "h":
case "hour":
return (long) (a*60*60*1000);
case "m":
case "min":
return (long) (a*60*1000);
case "s":
return (long) (a*1000);
case"ms":
return Math.round(a);
default:
return 0;
}
}
}