-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvectorTest.cpp
68 lines (65 loc) · 1.53 KB
/
vectorTest.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
int countAndSay(int k) {
vector<int> series;
vector<int> num_digits;
vector<int> num;
series.push_back(1);
series.push_back(11);
series.push_back(21);
int x;
// cout<<series[0];
// cout<<series[1];
// cout<<series[2];
for (int i = 3; i < k; i++) {
x = series[i - 1];
int length;
stack<int> sk;
do {
// cout << "inside 1. while: x = " << x << " x % 10 = " << x % 10 << endl;
sk.push(x % 10);
x = x / 10;
} while (x > 10);
sk.push(x);
length = sk.size();
int count = 1;
cout << sk.size() << endl;
while (!sk.empty()) {
// cout<<length<<endl;
num.push_back(sk.top());
sk.pop();
}
cout << "length: " << length << endl;
for (int j = 1; j <= length; j++) {
if (num[j] != num[j - 1]) {
num_digits.push_back(count);
num_digits.push_back(num[j - 1]);
count = 1;
} else
count++;
}
int exponent = num_digits.size() - 1;
cout << "num size " << num_digits.size() << endl;
series[i] = 0;
for (int v = 0; v < num_digits.size(); v++) {
series[i] += pow(10, exponent) * num_digits[v];
exponent--;
}
while (!num_digits.empty()) {
num_digits.pop_back();
}
while (!num.empty())
num.pop_back();
}
return series[k - 1];
}
int main() { cout << countAndSay(4); }