-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountAndSay.cpp
More file actions
41 lines (39 loc) · 869 Bytes
/
Copy pathcountAndSay.cpp
File metadata and controls
41 lines (39 loc) · 869 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
/**
*
*/
#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
string countAndSay(int n) {
if (n == 1) {
return "1";
}
string output = "";
string prev = countAndSay(n - 1);
char currNum = prev[0];
int currCount = 0;
for (char num : prev) {
if (num == currNum) {
currCount++;
} else {
output += currCount + '0';
output += currNum;
currNum = num;
currCount = 1;
}
}
output += currCount + '0';
output += currNum;
return output;
}
};
int main() {
int n;
cout << "Input n:" << endl;
cin >> n;
Solution solution;
string countAndSay = solution.countAndSay(n);
cout << countAndSay << endl;
}