-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMedianOfThree.java
More file actions
45 lines (37 loc) · 1.13 KB
/
MedianOfThree.java
File metadata and controls
45 lines (37 loc) · 1.13 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
public class MedianOfThree {
public static void main(String[] args) {
String[] input = Utils.getInputLines();
String results = "\nanswer\n";
for (int i = 0; i < input.length; i++) {
String[] numsStr = input[i].split(" ");
int[] nums = new int[numsStr.length];
for (int j = 0; j < numsStr.length; j++) {
nums[j] = Integer.parseInt(numsStr[j]);
}
results += medianOfThree(nums[0], nums[1], nums[2]);
if (i<input.length-1) {
results += " ";
}
}
System.out.println(results);
}
public static int medianOfThree(int num1, int num2, int num3) {
int swap;
if (num1 > num3) {
swap = num1;
num1 = num3;
num3 = swap;
}
if (num2 > num3) {
swap = num2;
num2 = num3;
num3 = swap;
}
if (num1 > num2) {
swap = num1;
num1 = num2;
num2 = swap;
}
return num2;
}
}