• 甲乙相距 n 公里,甲行進時速 x 公里,乙行進時速 y 公里,計算相遇時間
n=2000, x=15, y=19,亦即 (15+19) * hour >=2000
import java.util.Scanner;public class P4_5 {	public static void main(String[] args) {
		Scanner scn = new Scanner(System.in);		System.out.println("輸入距離(公里):");		int d = scn.nextInt();		System.out.println("輸入甲之行走時速(公里):");		int a = scn.nextInt();		System.out.println("輸入乙之行走時速(公里):");		int b = scn.nextInt();		int hours = 0;
		// 每過1小時,兩人相距剩餘之距離為:		// 總距離-(a每小時所走的距離+b每小時所走的距離)		// (a每小時所走的距離+b每小時所走的距離)乘以總時數大於總距離		// 其總時數即為所花費時數		if (d % (a + b) == 0) {			hours = (d / (a + b));		} else {			hours = (d / (a + b) + 1);		}
		System.out.printf("甲乙兩人相距 %d 公里\n甲時速 %d 公里,乙時速 %d                公里\n預計兩人將於 %d 小時內相遇", d, a, b, hours);		scn.close();	}}
  • 輸出結果:
輸入距離(公里):2000輸入甲之行走時速(公里):15輸入乙之行走時速(公里):19甲乙兩人相距 2000 公里甲時速 15 公里,乙時速 19 公里預計兩人將於 59 小時內相遇
arrow
arrow

    ALVIN 發表在 痞客邦 留言(0) 人氣()