Created
March 10, 2013 03:02
-
-
Save DanielDe/5126927 to your computer and use it in GitHub Desktop.
This file contains 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 <vector> | |
using namespace std; | |
int vsum(const vector<int> & v, unsigned start_index); | |
int main() { | |
vector<int> v; | |
v.push_back(3); | |
v.push_back(1); | |
v.push_back(4); | |
v.push_back(1); | |
v.push_back(5); | |
v.push_back(9); | |
cout << "Sum: " << vsum(v, 0) << endl; | |
return 0; | |
} | |
int vsum(const vector<int> & v, unsigned start_index) { | |
if (start_index >= v.size()) | |
return 0; | |
return v[start_index] + vsum(v, start_index + 1); | |
} |
This file contains 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> | |
using namespace std; | |
double pow(double b, int e); | |
int main() { | |
cout << pow(2.0, 4) << endl; | |
cout << pow(2.0, -4) << endl; | |
return 0; | |
} | |
double pow(double b, int e) { | |
if (b == 0) | |
return 0; | |
if (e == 0) | |
return 1; | |
if (e < 0) | |
return 1.0 / pow(b, -e); | |
return b * pow(b, e - 1); | |
} |
This file contains 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 <vector> | |
using namespace std; | |
void vprint(const vector<string> & v); | |
vector<string> permutations(string s); | |
int main() { | |
vprint(permutations("cat")); | |
return 0; | |
} | |
void vprint(const vector<string> & v) { | |
for (unsigned i = 0; i < v.size(); ++i) | |
cout << v.at(i) << endl; | |
} | |
vector<string> permutations(string s) { | |
if (s.size() == 0) { | |
vector<string> v; | |
v.push_back(""); | |
return v; | |
} | |
vector<string> lower_perms = permutations(s.substr(1)); | |
vector<string> perms; | |
for (unsigned i = 0; i < lower_perms.size(); ++i) { | |
string perm = lower_perms.at(i); | |
for (unsigned i = 0; i < perm.size() + 1; ++i) { | |
perms.push_back(perm.substr(0, i) + s.at(0) + perm.substr(i)); | |
} | |
} | |
return perms; | |
} |
This file contains 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> | |
using namespace std; | |
string string_sort(string s); | |
int main() { | |
cout << string_sort("fceatd") << endl; | |
return 0; | |
} | |
string string_sort(string s) { | |
if (s.size() == 0 || s.size() == 1) | |
return s; | |
string lower_sorted = string_sort(s.substr(1)); | |
unsigned i = 0; | |
for (; i < lower_sorted.size(); ++i) { | |
if (lower_sorted.at(i) > s.at(0)) | |
break; | |
} | |
return lower_sorted.substr(0, i) + s.at(0) + lower_sorted.substr(i); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment