-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShow the Sequence.cpp
46 lines (46 loc) · 1.08 KB
/
Show the Sequence.cpp
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
#include <stdio.h>
#include <string.h>
long long a[128];
void parsing(char s[]) {
int sign = 1, i, m = 0;
char pos;
for(i = 1; s[i]; i++) {
if(s[i] == '-') sign = -1;
else if(s[i] >= '0' && s[i] <= '9')
m = m * 10 + s[i] - '0';
else {
pos = s[i];
break;
}
}
m = sign * m;
if(pos == '+') {
parsing(s + i + 1);
long long d = a[0];
a[0] = m;
for(int i = 1; i < 50; i++) {
long long t = a[i];
a[i] = a[i - 1] + d;
d = t;
}
} else if(pos == '*') {
parsing(s + i + 1);
a[0] *= m;
for(int i = 1; i < 50; i++)
a[i] = a[i] * a[i-1];
} else {
for(int i = 0; i < 50; i++)
a[i] = m;
}
}
int main() {
char s[1024];
int n;
while(scanf("%s %d", s, &n) == 2) {
memset(a, 0, sizeof(a));
parsing(s);
for(int i = 0; i < n; i++)
printf("%lld%c", a[i], i == n - 1 ? '\n' : ' ');
}
return 0;
}