Eason
BAN USERHello, CareerCup!
count and say!
#include <bits/stdc++.h>
using namespace std;
string getNext(string s) {
string res;
int cnt = 1;
for (int i = 1; i <= s.length(); ++i) {
if (i < s.length() && s[i] == s[i-1]) {
++cnt;
} else {
res += to_string(cnt) + s[i-1];
cnt = 1;
}
}
return res;
}
string say(int n) {
string res = "1";
for (int i = 1; i < n; ++i) {
res = getNext(res);
}
return res;
}
int main() {
int n;
while (cin >> n) {
cout << say(n) << endl;
}
return 0;
}
- Eason May 06, 2015my solution:
#include <iostream>
using namespace std;
int min(int a, int b) {
return a < b ? a : b;
}
int main() {
int N;
cin >> N;
int t = 0;
int sum = 0;
int idx3 = 1, idx5 = 1;
while (true) {
t = min(3*idx3, 5*idx5);
if (t < 3 * idx3) {
++idx5;
} else if (t < 5 * idx5) {
++idx3;
} else {
++idx3;
++idx5;
}
if (t < N) {
sum += t;
} else {
break;
}
}
cout << sum << endl;
return 0;
}
Morris Traversal,here is the AC code of Leetcode #94
- Eason May 06, 2015