-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.cpp
49 lines (41 loc) · 794 Bytes
/
main.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
// Author : Qi Zhang
// Date : 2018-12-11
#include <bits/stdc++.h>
using namespace std;
int magicalString(int n) {
string s = "122";
char w = '1';
int i = 2, t = 3;
while(t <= n)
{
if(w == '1')
{
int x = s[i]-'0';
s += (x==1 ? "1" : "11");
t += x;
w = '2';
i++;
}
else
{
int x = s[i]-'0';
s += (x==1 ? "2" : "22");
t += x;
w = '1';
i++;
}
}
int res = 0;
for(int i = 0; i < n; i++)
if(s[i] == '1') res++;
return res;
}
int main()
{
string line;
while (getline(cin, line)) {
int n = stoi(line);
cout << magicalString(n) << endl;
}
return 0;
}