-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClimbingStairs.java
More file actions
54 lines (46 loc) · 1.2 KB
/
ClimbingStairs.java
File metadata and controls
54 lines (46 loc) · 1.2 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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigInteger;
public class ClimbingStairs {
private BigInteger countWays(int n) {
if (n == 1 || n == 2) {
return BigInteger.valueOf(n);
}
int i;
BigInteger j = BigInteger.valueOf(3);
BigInteger k = BigInteger.valueOf(2);
BigInteger tmp;
for (i = 3; i < n; i++) {
tmp = j;
j = j.add(k);
k = tmp;
}
return j;
}
public ClimbingStairs(String filename) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(filename));
String line;
int stairs;
while ((line = br.readLine()) != null) {
try {
stairs = Integer.valueOf(line).intValue();
if (stairs < 1) {
System.out.println("Not a valid case. Skipping.");
continue;
}
System.out.println(countWays(stairs));
} catch (NumberFormatException e) {
System.out.println("Not a number. Going to next line.");
continue;
}
}
}
public static void main (String[] args) throws IOException {
if (args.length != 1) {
System.out.println("Insufficient number of parameters passed. Exiting.");
System.exit(1);
}
ClimbingStairs cs = new ClimbingStairs(args[0]);
}
}