-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
executable file
·142 lines (121 loc) · 3.54 KB
/
Copy pathMain.java
File metadata and controls
executable file
·142 lines (121 loc) · 3.54 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//package StackingCylinders;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class Main {
List<Coordinate> currentRow;
public Main(double[] numbers) {
currentRow = new LinkedList<Coordinate>();
Arrays.sort(numbers);
for (double xCoordinate: numbers) {
currentRow.add(new Coordinate(xCoordinate, 1));
}
}
private Coordinate getNextCylinder(Coordinate f, Coordinate s) {
double p, q;
double r = 2;
if (Math.abs(f.y - s.y) < Math.pow(10, -12)) {
p = (f.x + s.x) / 2;
q = f.y + Math.sqrt(Math.pow(r, 2) - Math.pow(f.x - p, 2));
} else {
// q = ap + b where (p, q) is the new coordinate.
double a = (s.x - f.x) / (f.y - s.y);
double b = (Math.pow(f.x, 2) - Math.pow(s.x, 2) + Math.pow(f.y, 2) - Math
.pow(s.y, 2)) / 2 / (f.y - s.y);
double j = 1 + Math.pow(a, 2);
double k = 2 * (a * b - a * f.y - f.x);
double l = Math.pow(f.x, 2) + Math.pow(f.y - b, 2) - Math.pow(r, 2);
//choose bigger q
if (a >= 0) {
p = pformula(j,k,l);
}
else {
p = mformula(j,k,l);
}
q = a * p + b;
}
//System.out.println(p + "|" + q);
return new Coordinate(p, q);
}
public void execute() {
while (currentRow.size() != 1) {
List<Coordinate> nextRow = new LinkedList<Coordinate>();
for (int i = 0; i < currentRow.size() - 1; i++) {
nextRow.add(getNextCylinder(currentRow.get(i),
currentRow.get(i + 1)));
}
currentRow = nextRow;
}
Coordinate topmost = currentRow.get(0);
System.out.printf("%.4f %.4f\n", topmost.x, topmost.y);
}
private double roundAt(double x, int digit) {
double constant = Math.pow(10, digit);
return Math.round(x * constant) / constant;
}
/*
* return greater one.
*/
private double pformula(double a, double b, double c) {
return (-1 * b + Math.sqrt(Math.pow(b, 2) - 4 * a * c))/(2 * a);
}
/*
* return smaller one.
*/
private double mformula(double a, double b, double c) {
return (-1 * b - Math.sqrt(Math.pow(b, 2) - 4 * a * c))/(2 * a);
}
public static void readInput() {
try {
//BufferedReader bf = new BufferedReader(new FileReader("./src/StackingCylinders/input.txt"));
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String input;
while ((input = bf.readLine()) != null) {
String [] words = input.split(" ");
int numberOfInput = Integer.parseInt(words[0]);
if (numberOfInput == 0) {
break;
}
double[] numbers = new double[numberOfInput];
for (int i = 1; i < words.length; i++) {
numbers[i - 1] = Double.parseDouble(words[i]);
}
Main z = new Main(numbers);
z.execute();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void readInputByScanner() {
try {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int numberOfInput = sc.nextInt();
if (numberOfInput == 0) break;
double[] numbers = new double[numberOfInput];
for (int i = 0; i < numberOfInput; i++) {
numbers[i] = sc.nextDouble();
}
Main z = new Main(numbers);
z.execute();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Main.readInputByScanner();
}
}
class Coordinate {
public double x, y;
public Coordinate(double x, double y) {
this.x = x;
this.y = y;
}
}