-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcountAndSay.cpp
More file actions
49 lines (45 loc) · 949 Bytes
/
countAndSay.cpp
File metadata and controls
49 lines (45 loc) · 949 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
49
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
string countAndSay(int n) {
string a="11",b,c;
if(n==1){return "1";}
if(n==2){return "11";}
for(int i=2;i<n;i++)
{
int nt=a.length();
b="";
int count=1;
for(int j=1;j<nt;j++)
{
if(a[j]==a[j-1]){
count++;
}
else{
b=b+to_string(count)+a[j-1];
count=1;
}
}
b=b+to_string(count)+a[nt-1];
a=b;
}
return a;
}
};
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
Solution ob;
cout<<ob.countAndSay(n);
}
return 0;
}