close
題目概要:
給定目前時間,跟目前速度(如果只有時間,則速度不改變),求出目前所走的距離。
解題方向:
時間*速度=距離。
程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Java | |
import java.util.Scanner; | |
class uva10281{ | |
public static void main(String args[]){ | |
Scanner sc=new Scanner(System.in); | |
double km=0,v=0,v1=0,time=0,time2=0; | |
int startTime=0; | |
while(sc.hasNextLine()){ | |
int i=0; | |
String st[]=sc.nextLine().split(" "); //時間、速度分開。 | |
while(st[i].equals(""))i++; | |
//時間換算成秒。 | |
String stString[]=st[i].split(":"); | |
time=Double.parseDouble(stString[0])*3600+Double.parseDouble(stString[1])*60+Double.parseDouble(stString[2])*1; | |
km+=(time-time2)*v/3600; //距離計算。 | |
time2=time; | |
//Output | |
if(st.length==1){ | |
System.out.printf("%s %.2f km",st[0],km); | |
System.out.println(""); | |
}else{ | |
v=Double.parseDouble(st[i+1]); | |
} | |
} | |
} | |
} |
文章標籤
全站熱搜