-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCount and Say.cpp
62 lines (60 loc) · 1.32 KB
/
Count and Say.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
#include<iostream>
#include<string>
using namespace std;
string countAndSay(int n)
{
if (n==0)
return "";
string re="1";
string cont;
for (int i=0;i<n-1;i++)
{
int num=1;
for (int j=0;j<re.size();)
{
while (re[j]==re[j+1]) {j++;num++;}
cont.push_back('0'+num);
cont.push_back(re[j]);
j++;
num=1;
}
re.assign(cont);
cout<<re<<endl;
cont.clear();
}
return re;
}
//string countAndSay(int n) {
// string temp, result;
// result.push_back('1');
//
// char currentChar;
// int count;
//
// for(int i = 1; i < n; i++) {
// temp = result;
// result.clear();
// currentChar = temp[0];
// count = 1;
// for(int j = 1; j < temp.size(); j++) {
// if(temp[j] == currentChar) {
// count++;
// }
// else {
// result.push_back('0' + count);
// result.push_back(currentChar);
// currentChar = temp[j];
// count = 1;
// }
// }
// result.push_back('0' + count);
// result.push_back(currentChar);
// }
// return result;
// }
int main()
{
string c=countAndSay(6);
cout<<c;
return 0;
}