-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathsolution038.cpp
More file actions
48 lines (45 loc) · 811 Bytes
/
solution038.cpp
File metadata and controls
48 lines (45 loc) · 811 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
/**
* Count and Say
*
* cpselvis([email protected])
* September 12th, 2016
*/
#include<iostream>
using namespace std;
class Solution {
public:
string countAndSay(int n) {
string ret = "1";
for (int i = 1; i < n; i ++)
{
int count = 1;
string tmp = "";
for (int j = 1; j < ret.size(); j ++)
{
if (ret[j] == ret[j - 1])
{
count ++;
}
else
{
tmp += to_string(count);
tmp += ret[j - 1];
count = 1;
}
}
tmp += to_string(count);
tmp += ret[ret.size() - 1];
ret = tmp;
}
return ret;
}
};
int main(int argc, char **argv)
{
Solution s;
cout << s.countAndSay(1) << endl;
cout << s.countAndSay(2) << endl;
cout << s.countAndSay(3) << endl;
cout << s.countAndSay(4) << endl;
cout << s.countAndSay(5) << endl;
}