-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCarRace.java
More file actions
45 lines (36 loc) · 1.27 KB
/
Copy pathCarRace.java
File metadata and controls
45 lines (36 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package Lists;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
public class CarRace {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Integer> carRace = Arrays.stream(scanner.nextLine().split(" "))
.map(Integer::parseInt).collect(Collectors.toList());
int middleIndex = carRace.size()/ 2;
double firstCar = 0;
double secondCar = 0;
for (int i = 0; i < middleIndex; i++) {
int element = carRace.get(i);
if(element != 0){
firstCar += element;
}else{
firstCar = firstCar - firstCar * 0.20;
}
}
for (int i = carRace.size() - 1; i > middleIndex ; i--) {
int element = carRace.get(i);
if(element != 0){
secondCar += element;
}else{
secondCar = secondCar - secondCar * 0.20;
}
}
if(firstCar < secondCar){
System.out.printf("The winner is left with total time: %.1f",firstCar);
}else if(secondCar < firstCar){
System.out.printf("The winner is right with total time: %.1f",secondCar);
}
}
}