Last active
September 23, 2016 02:43
-
-
Save yusuke024/6d8c15f97b4b9fbdba2d032d20bf07e7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
std::vector<std::string> comb(std::string s, int n) { | |
std::vector<std::string> v; | |
if (s.size() < n || n <= 0) return v; | |
auto ss = s.substr(0, 1); | |
auto vv = comb(s.substr(1), n-1); | |
if (vv.size() == 0) { | |
v.push_back(ss); | |
} | |
for (auto si: vv) { | |
v.push_back(ss + si); | |
} | |
vv = comb(s.substr(1), n); | |
for (auto si: vv) { | |
v.push_back(si); | |
} | |
return v; | |
} | |
int main(int argc, char const *argv[]) { | |
auto v = comb(argv[1], std::stoi(argv[2])); | |
for (auto s: v) { | |
std::cout << s << "\n"; | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#define abs(x) ((x) >= 0 ? (x) : -(x)) | |
int main(int argc, char const *argv[]) { | |
int n = 5; | |
for (int i = 0; i < n*2-1; ++i) { | |
int p = (n-1) - abs(i - (n-1)); | |
std::cout << p << ","; | |
} | |
std::cout << std::endl; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
int abs(int n) { | |
return n >= 0 ? n : -n; | |
} | |
std::string pattern(int n) | |
{ | |
if (n < 1) return ""; | |
std::string s; | |
for (int j = 0; j < n*2 - 1; ++j) { | |
int i = n - abs(n - j - 1) - 1; | |
auto p = std::to_string((i+1) % 10); | |
s += std::string(i, ' ') + p + std::string(n-i-1, ' '); | |
s += std::string(i == n-1 ? 0 : n-i-2, ' ') + (i == n-1 ? "" : p) + std::string(i, ' ') + "-\n"; | |
} | |
return s; | |
} | |
int main(int argc, char const *argv[]) { | |
std::cout << pattern(10) << std::endl; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
std::vector<std::string> perm(std::string s) { | |
std::vector<std::string> v; | |
if (s.size() == 0) { | |
v.push_back(""); | |
return v; | |
} | |
auto ss = s.substr(0, 1); | |
auto vv = perm(s.substr(1, s.size()-1)); | |
for (std::string si: vv) { | |
for (int i = 0; i <= si.size(); ++i) { | |
v.push_back(si.substr(0, i) + ss + si.substr(i, si.size()-i)); | |
} | |
} | |
return v; | |
} | |
int main(int argc, char const *argv[]) { | |
auto v = perm(argv[1]); | |
for (auto s: v) { | |
std::cout << s << "\n"; | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
using namespace std; | |
int countBits(int n) { | |
int c = 0; | |
for (int i = 0; i < sizeof(n) * 8; ++i) { | |
if ((n & (1 << i)) != 0) { | |
++c; | |
} | |
} | |
return c; | |
} | |
class Solution { | |
public: | |
vector<string> readBinaryWatch(int num) { | |
vector<string> v; | |
for (int i = 0; i < (1 << 10); ++i) { | |
int hc = countBits(i & 960), mc = countBits(i & 63); | |
if (hc > 3 || mc > 5 || (hc + mc) != num) continue; | |
int h = i >> 6, m = i & 63; | |
v.push_back(to_string(h) + ":" + (m < 10 ? "0" : "") + to_string(m)); | |
} | |
return v; | |
} | |
}; | |
int main(int argc, char const *argv[]) { | |
auto v = Solution().readBinaryWatch(1); | |
for (auto s: v) { | |
cout << s << "\n"; | |
} | |
cout << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment