UVa 12842 - The Courier Problem

contents

  1. 1. Problem
  2. 2. Sample Input
  3. 3. Sample Output
  4. 4. Solution

Problem

軍隊筆直等速度移動,隊伍長度 L,信使位於隊伍最後面,要傳遞訊息到隊伍前方後跑回隊伍末端。跑回末端時候,隊伍已經前進 L 距離。求信使跑的距離為何?

Sample Input

1
2
1
50

Sample Output

1
Case 1: 120.71

Solution

假設軍隊移動速度$v$, 信使移動速度$u$

看時間

$$\frac{L}{v} = \frac{L}{u-v} + \frac{L}{u+v} \\ u^{2} - v^{2} = 2v, \\ \text{Let u = 1, get } v = \frac{-2 + \sqrt{8} }{2}$$

輸出$\frac{L}{v} u$ 即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <math.h>
int main() {
int testcase, cases = 0;
double L;
double u = 1, v = (-2 + sqrt(8))/ 2.0;
scanf("%d", &testcase);
while (testcase--) {
scanf("%lf", &L);
double time = L / v;
printf("Case %d: %.2lf\n", ++cases, time * u);
}
return 0;
}
/*
army v, courier u
time:
L / v = L / (u - v) + L / (u + v) => u^2 - v^2 = 2v
result L / v * u, let u = 1 => v = (-2 + sqrt(8))/ 2.0.
*/