-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSequences.java
More file actions
48 lines (42 loc) · 833 Bytes
/
Sequences.java
File metadata and controls
48 lines (42 loc) · 833 Bytes
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
/**
* Name : Yu Chenghui
* Matric. No : A0194474U
* PLab Acct. :
*/
import java.util.*;
public class Sequences {
private void run() {
//use 'DR' to express number D or R
int N, T;
long A, DR;
int i;
long num;
Scanner sc = new Scanner(System.in);
// read input
N = sc.nextInt();
T = sc.nextInt();
A = sc.nextLong();
DR = sc.nextLong();
// loop to generate numbers
for (i = 0; i < N; i++) {
if (T == 1) {
// arithmatic sequence
System.out.print(A + i * DR);
} else {
// geometric sequence
System.out.print(A);
A = A * DR;
}
// format of output
if (i < N - 1) {
System.out.print(" ");
} else {
System.out.println();
}
}
}
public static void main(String[] args) {
Sequences newSequences = new Sequences();
newSequences.run();
}
}