王道例题8.2 汉诺塔Ⅲ
描述
思路
假设已经将前n-1个圆盘移动至最右
- 将n-1个圆盘移动至最右侧
- 将第n个圆盘移动至中间
- 将n-1个圆盘移动至最左
- 将第n个圆盘移动至最右
- 将n-1个圆盘移动至最右侧
总共进行了3次F(n-1)和两次操作
代码
#include<iostream>
#include<string>
#include<vector>
#include<cstdio>
using namespace std;
int Function(int x) {
if (x == 1)
return 2;
else
return Function(x - 1) * 3 + 2;
}
int main() {
int a;
while (cin >> a) {
cout << Function(a) << endl;
}
return 0;
}
KY96 Fibonacci
描述
The Fibonacci Numbers{0,1,1,2,3,5,8,13,21,34,55…} are defined by the recurrence: F0=0 F1=1 Fn=Fn-1+Fn-2,n>=2 Write a program to calculate the Fibonacci Numbers.
代码
#include <iostream>
using namespace std;
int F(int x){
if(x == 0 || x == 1)
return x;
else
return F(x -1) + F(x - 2);
}
int main() {
int x;
while (cin >> x) {
cout << F(x);
}
return 0;
}
KY85 二叉树
描述
如上所示,由正整数1,2,3……组成了一颗特殊二叉树。我们已知这个二叉树的最后一个结点是n。现在的问题是,结点m所在的子树中一共包括多少个结点。比如,n = 12,m = 3那么上图中的结点13,14,15以及后面的结点都是不存在的,结点m所在子树中包括的结点有3,6,7,12,因此结点m的所在子树中共有4个结点。
思路
使用递归函数时,需要考虑边界条件,既退出递归的条件
代码
#include <iostream>
using namespace std;
int F(int m, int n) {
if (m > n) {
return 0;
} else
return 1 + F(2 * m, n) + F(2 * m + 1, n);
}
int main() {
int m, n, count = 0;
while (cin >> m >> n) {
if(m == 0 && n == 0)
break;
cout << F(m, n) << endl;
}
return 0;
}
字符串去除重复字符
代码
#include<string>
#include<algorithm>
class Solution {
public:
string NS_String(string s, int k) {
string temp = s;
string sor;
sort(s.begin(), s.end());
sor.push_back(s[0]);
for (int q = 0; q < s.size() - 1; q++) {
if (s[q] != s[q + 1])
sor.push_back(s[q + 1]);
}
for (int u = 0; u < k; u++) {
for (int j = 0; j < temp.size(); j++) {
if (temp[j] == sor[u]) {
temp.erase(j, 1);
j = j - 1;
}
}
}
return temp;
}
};
KY103 2的幂次方
描述
Every positive number can be presented by the exponential form.For example, 137 = 2^7 + 2^3 + 2^0.
Let’s present a^b by the form a(b).Then 137 is presented by 2(7)+2(3)+2(0). Since 7 = 2^2 + 2 + 2^0 and 3 = 2 + 2^0 , 137 is finally presented by 2(2(2)+2 +2(0))+2(2+2(0))+2(0).
Given a positive number n,your task is to present n with the exponential form which only contains the digits 0 and 2.
思路
先将输入数转换为二进制字符串,再将它们转换为结果
转二进制用字符串除法
代码
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
void pri(int s) {
vector<int> bin;
while (s) {
bin.push_back(s % 2);
s /= 2;
}
bool flag = true;
for (int i = bin.size() - 1; i >= 0; i--) {
if (bin[i] == 1) {
if (!flag) {
cout << "+";
}
flag = false;
if (i == 1) {
cout << "2";
} else if (i == 0) {
cout << "2(0)";
} else {
cout << "2(";
pri(i);
cout << ")";
}
}
}
}
int main() {
int s;
while (cin >> s) {
pri(s);
}
return 0;
}
KY11 二叉树遍历
描述
编一个程序,读入用户输入的一串先序遍历字符串,根据此字符串建立一个二叉树(以指针方式存储)。 例如如下的先序遍历字符串: ABC##DE#G##F### 其中“#”表示的是空格,空格字符代表空树。建立起此二叉树以后,再对二叉树进行中序遍历,输出遍历结果。
代码
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;
struct treeNode {
char data;
treeNode* leftChild;
treeNode* rightChild;
treeNode(char c): data(c), leftChild(NULL), rightChild(NULL){}
};
treeNode* InsertTreeNode(int& position, string str) {
char c = str[position++];
if (c == '#')
return NULL;
treeNode* root = new treeNode(c);
root->leftChild = InsertTreeNode(position, str);
root->rightChild = InsertTreeNode(position, str);
return root;
}
void InOrder(treeNode* root) {
if (root == NULL)
return;
InOrder(root->leftChild);
cout << root->data << " ";
InOrder(root->rightChild);
return;
}
int main() {
string str;
while (cin >> str) {
int position = 0;
treeNode* root = InsertTreeNode(position, str);; //指向跟结点
InOrder(root);
cout << endl;
}
return 0;
}
王道例题 层次遍历
思路
需要用到一个辅助队列
先访问根节点,将根节点所有的左右子树加入队列
代码
#include<cstdio>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
struct treeNode {
char data;
treeNode* leftChild;
treeNode* rightChild;
treeNode(char c) : data(c), leftChild(NULL), rightChild(NULL) {}
};
treeNode* InsertTreeNode(int& position, string str) {
char c = str[position++];
if (c == '#')
return NULL;
treeNode* root = new treeNode(c);
root->leftChild = InsertTreeNode(position, str);
root->rightChild = InsertTreeNode(position, str);
return root;
}
treeNode* Build(int& position, string str) {
char c = str[position++];
if (c == '#') {
return NULL;
}
treeNode* root = new treeNode(c);
root->leftChild = Build(position, str);
root->rightChild = Build(position, str);
return root;
}
void LevelOrder(treeNode* root) {
queue<treeNode*> pos;
pos.push(root);
while (pos.empty() == false) {
treeNode* Pt = pos.front();
pos.pop();
cout << Pt->data;
if (Pt->leftChild != NULL)
pos.push(Pt->leftChild);
else if (Pt->rightChild != NULL)
pos.push(Pt->rightChild);
}
}
int main() {
string str;
while (cin >> str) {
int position = 0;
treeNode* root = Build(position, str); //指向跟结点
LevelOrder(root);
cout << endl;
}
return 0;
}