liuxuli
BAN USER<pre lang="c++" line="1" title="CodeMonkey83653" class="run-this">struct node {
int val;
node *left;
node *right;
};
void print_leaves (node *n) {
if (!n->left && !n->right)
cout << n->val << " ";
else {
if (n->left)
print_leaves (n->left);
if (n->right)
print_leaves (n->right);
}
}
void print_left_edge (node *n) {
cout << n->val << " ";
if (n->left)
print_left_edge (n->left);
if (n->right)
print_leaves (n->right);
}
void print_right_edge (node *n) {
if (n->left)
print_leaves (n->left);
if (n->right)
print_right_edge (n->right);
cout << n->val << " ";
}
void print_border (node *root) {
if (root->left)
print_left_edge(root->left);
if (root->right)
print_right_edge(root->right);
cout << root->val << endl;
}
</pre><pre title="CodeMonkey83653" input="yes">
</pre>
<pre lang="c++" line="1" title="CodeMonkey87449" class="run-this">void merge (node *tree1, node *tree2) {
if (tree2->val <= tree1->val) {
node *right_child = tree2->right;
tree2->right = NULL;
if (!tree1->left) {
tree1->left = tree2;
} else {
merge(tree1->left, tree2);
}
if (right_child)
merge(tree1, right_child);
} else {
node *left_child = tree2->left;
tree2->left = NULL;
if (!tree1->right) {
tree1->right = tree2;
} else {
merge(tree1->right, tree2);
}
if (left_child)
merge(tree1, left_child);
}
}
</pre><pre title="CodeMonkey87449" input="yes">
</pre>
<pre lang="c++" line="1" title="CodeMonkey85725" class="run-this">bool existing (const vector<int>& numbers, int goal) {
if (numbers.size() == 1) {
if (numbers[0] == goal)
return true;
else
return false;
}
for (int i=0; i<numbers.size(); i++) {
vector<int> numbers_left;
for (int j=0; j<numbers.size(); j++) {
if (i != j)
numbers_left.push_back(numbers[j]);
}
if (existing (numbers_left, goal-numbers[i]))
return true;
if (existing (numbers_left, goal+numbers[i]))
return true;
if (existing (numbers_left, goal/numbers[i]))
return true;
if (existing (numbers_left, goal*numbers[i]))
return true;
}
return false;
}
</pre><pre title="CodeMonkey85725" input="yes">
</pre>
<pre lang="c++" line="1" title="CodeMonkey36722" class="run-this">vector<int> num2list(int n) {
- liuxuli September 27, 2010vector<int> result;
while (n) {
result.push_back(n%10);
n /= 10;
}
return result;
}
vector<int> multiply_list(const vector<int> &list1, const vector<int> &list2) {
vector<int> result;
int start_from = 0;
for (int i=0; i<list2.size(); i++) {
int carry_in = 0;
for (int j=0; j<list1.size(); j++) {
int product = list2[i] * list1[j];
if (start_from+j < result.size()) {
result[start_from+j] = (product + carry_in) % 10;
carry_in = (product + carry_in) / 10;
} else {
result.push_back((product + carry_in) % 10);
}
carry_in = (product + carry_in) / 10;
}
if (carry_in)
result.push_back(carry_in);
start_from++;
}
return result;
}
vector<int> factorial(int n) {
vector<int> result(1, 1);
for (int i=2; i<=n; i++) {
vector<int> multiplier = num2list(i);
result = multiply_list(result, multiplier);
}
reverse(result.begin(), result.end());
return result;
}
</pre><pre title="CodeMonkey36722" input="yes">
</pre>