-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathsolve.cpp
57 lines (55 loc) · 932 Bytes
/
solve.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
#include <string>
#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
using namespace std;
class Solution {
public:
string countAndSay(int n) {
n--;
string result = "1";
while (n--) {
result = next(result);
}
return result;
}
private:
string next(string s) {
int n = s.size();
if (n < 1)
return "";
char cur = s[0];
int count = 1;
string result;
for (int i = 1; i < n; ++i) {
if (cur != s[i]) {
result += itos(count);
result.push_back(cur);
cur = s[i];
count = 1;
} else {
++count;
}
}
result += itos(count);
result += cur;
return result;
}
string itos(int i) {
ss.str("");
ss.clear();
ss << i;
return ss.str();
}
stringstream ss;
};
int main(int argc, char **argv)
{
Solution solution;
int n;
while (scanf("%d", &n) != EOF) {
cout << solution.countAndSay(n) << endl;
}
return 0;
}