-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
35 lines (33 loc) · 797 Bytes
/
main.cpp
File metadata and controls
35 lines (33 loc) · 797 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
#include <iostream>
#include <unordered_set>
#include <vector>
std::string countAndSay(int n) {
if (n == 1) {
return "1";
}
std::string result {"1"};
for (int i = 1; i <n; i++) {
std::string temp;
int count = 1;
char prev = result[0];
for (int j = 1; j < result.size(); j++) {
const char next = result[j];
if (prev != next) {
temp += std::to_string(count);
temp += prev;
count = 1;
prev = next;
} else {
count++;
}
}
temp += std::to_string(count);
temp += prev;
result = temp;
}
return result;
}
int main() {
std::cout << countAndSay(4) << std::endl;
return 0;
}