-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c255943
commit 364431a
Showing
28 changed files
with
1,123 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include<iostream> | ||
#include<cstring> | ||
#include<algorithm> | ||
using namespace std; | ||
|
||
char A_char[1000], B_char[1000]; | ||
int A_int[1000] = {0}, B_int[1000] = {0}, sum[1001]; | ||
|
||
int main(){ | ||
cin >> A_char >> B_char; | ||
int len1, len2; | ||
len1 = strlen(A_char), len2 = strlen(B_char); | ||
reverse(A_char, A_char + len1); | ||
reverse(B_char, B_char + len2); | ||
for(int i = 0; i < len1; i++) | ||
A_int[i] = A_char[i] - '0'; | ||
for(int i = 0; i < len2; i++) | ||
B_int[i] = B_char[i] - '0'; | ||
int c = 0; //进位位 | ||
for(int i = 0; i <= len1 || i <= len2; i++){ //此处详解见末尾 | ||
sum[i] = (A_int[i] + B_int[i] + c) % 10; //当前位的值 | ||
c = (A_int[i] + B_int[i] + c) / 10; //当前为的进位值 | ||
} | ||
int len = len1 > len2 ? len1 : len2; //得到最终和的长度 | ||
for(int i = len; i >= 0; i--){ //逆序输出 | ||
if(i == len && sum[i] == 0) //最高位为0不输出 | ||
continue; | ||
cout << sum[i]; | ||
} | ||
cout << endl; | ||
return 0; | ||
} | ||
|
||
/*关于按位加法循环条件的说明: | ||
这里循环条件中的<=与前面的初始化数组全为0一起保证能正常处理进位 | ||
例如若len1 < len2,len1 <= i <= len2时,A_int[i]实际不是 | ||
数A的有效位,但它们都为0,可与数B的有效位相加实现正常的进位,同 | ||
时也保证了最高位的进位 | ||
*/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include<iostream> | ||
using namespace std; | ||
|
||
int convert(char num[]){ | ||
int sum = 0; | ||
int i = 0; | ||
bool flag = true; //默认为正数 | ||
if(num[0] == '-'){ | ||
i++; | ||
flag = false; | ||
} //第一位为'-',为负数 | ||
while(num[i]){ | ||
if(num[i] == ','){ | ||
i++; continue; | ||
} | ||
int n = num[i] - '0'; | ||
sum = sum * 10 + n; | ||
i++; | ||
} | ||
if(flag) | ||
return sum; | ||
else | ||
return -sum; | ||
} | ||
|
||
int main(){ | ||
char A[10], B[10]; | ||
cin >> A >> B; | ||
int a = convert(A); | ||
int b = convert(B); | ||
cout << a + b <<endl; | ||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
//重建二叉树后进行后序遍历输出 | ||
#include<iostream> | ||
using namespace std; | ||
|
||
typedef struct Node{ | ||
char cha; | ||
Node *lchild, *rchild; | ||
}*BT; //树结点,用于重建二叉树 | ||
|
||
char preQue[26], inQue[26]; //前序、中序遍历序列 | ||
|
||
BT reBuild(int preL, int preH, int inL, int inH){ //参数分别为需要重建的子树在前序、中序序列中的起止位置 | ||
if(preL > preH || inL > inH) //子树不存在,到达尽头 | ||
return NULL; | ||
Node* root = new Node; //新建当前子树根结点 | ||
root->cha = preQue[preL]; //根结点为前序序列的第一个点 | ||
int pos; | ||
for(pos = inL; pos <= inH; pos++){ | ||
if(root->cha == inQue[pos]) | ||
break; | ||
} //找到根结点在中序序列中的位置 | ||
int len = pos - inL; //该根结点的左子树结点数 | ||
root->lchild = reBuild(preL + 1, preL + len, inL, pos - 1); //重建左子树 | ||
root->rchild = reBuild(preL + len + 1, preH, pos + 1, inH); //重建右子树 | ||
return root; | ||
} | ||
void postOrder(BT T){ | ||
if(T->lchild) | ||
postOrder(T->lchild); | ||
if(T->rchild) | ||
postOrder(T->rchild); | ||
cout << T->cha; | ||
} | ||
|
||
int main(){ | ||
while(cin >> preQue >> inQue){ | ||
int l; | ||
for(l = 0; preQue[l] != '\0'; l++); | ||
BT T = reBuild(0, l, 0, l); | ||
postOrder(T); | ||
cout << endl; | ||
delete T; | ||
} | ||
return 0; | ||
} | ||
|
||
|
||
/* | ||
//直接得到后序遍历序列,不重建二叉树 | ||
#include<iostream> | ||
using namespace std; | ||
char preQue[26], inQue[26], postQue[26]; | ||
int p = 0; //插入点在后序序列中的位置 | ||
void Post(int preL, int preH, int inL, int inH){ | ||
if(preL > preH || inL > inH) //到达尽头 | ||
return; | ||
int pos; | ||
for(pos = inL; pos <= inH; pos++){ | ||
if(preQue[preL] == inQue[pos]) | ||
break; | ||
} //找到当前根结点在中序序列中的位置 | ||
int len= pos - inL; | ||
Post(preL + 1, preL + len, inL, pos - 1); //递归处理左子树 | ||
Post(preL + len + 1, preH, pos + 1, inH); //递归处理右子树 | ||
postQue[p++] = preQue[preL]; //将当前根结点存入后序序列 | ||
} | ||
int main(){ | ||
while(cin >> preQue >> inQue){ | ||
int l = 0; | ||
while(preQue[l++]); | ||
Post(0, l, 0, l); | ||
for(int i = 0; i <= l; i++) | ||
cout << postQue[i]; | ||
cout << endl; | ||
p = 0; //重置插入点位置供下一组输入用 | ||
} | ||
return 0; | ||
} | ||
*/ | ||
|
||
|
||
/*--------看到的一种使用STL实现的简洁方法 | ||
#include<iostream> | ||
#include<string> | ||
using namespace std; | ||
void Post(string str1, string str2){ | ||
if(str1.length() == 0) | ||
return; | ||
int len = str2.find(str1[0]); //得到左子树结点数 | ||
Post(str1.substr(1, len), str2.substr(0, len)); //递归处理左子树 | ||
Post(str1.substr(1 + len), str2.substr(len + 1)); //递归处理右子树 | ||
cout<<str1[0]; | ||
} | ||
int main(){ | ||
string pre, in; | ||
while(cin >> pre >> in){ | ||
Post(pre, in); | ||
} | ||
return 0; | ||
} | ||
*/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include<iostream> | ||
#include<string> | ||
#include<algorithm> | ||
using namespace std; | ||
|
||
bool cmp(string a, string b){ | ||
if(a.length() != b.length()) | ||
return a.length() < b.length(); //a、b不等长时,短的更小 | ||
else | ||
return a < b; | ||
} //比较函数 | ||
|
||
int main(){ | ||
int N; | ||
string str[100]; | ||
while(cin>>N){ | ||
for(int i = 0; i < N; i++){ | ||
cin>>str[i]; | ||
} | ||
sort(str, str + N, cmp); //排序 | ||
for(int i = 0; i < N; i++){ | ||
cout<<str[i]<<endl; | ||
} | ||
} | ||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#include<iostream> | ||
|
||
using namespace std; | ||
|
||
#define MAXSIZE 100 | ||
typedef struct treeNode{ | ||
int data; | ||
int lchild, rchild, father; | ||
}BST; | ||
int pos = 0; //插入点指针,若插入结点,其在整个结构数组中的位置为pos | ||
void Initi(BST T[]){ | ||
for(int i = 0; i < MAXSIZE; i++){ | ||
T[i].data = 0; | ||
T[i].lchild = T[i].rchild = T[i].father = -1; | ||
} | ||
} //初始化结构数组 | ||
void Insert(BST T[], int num){ | ||
if(T[0].data == 0){ | ||
T[0].data = num; | ||
} //空树,插入根结点 | ||
else{ | ||
T[pos].data = num; | ||
T[pos].lchild = T[pos].rchild = -1; //取出结构数组中的一个元素并初始化 | ||
int loc = 0; | ||
while(true){ | ||
if(num > T[loc].data){ | ||
if(T[loc].rchild != -1) | ||
loc = T[loc].rchild; | ||
else{ | ||
T[loc].rchild = pos, T[pos].father = loc; break; | ||
} | ||
} //向右到尽头并插入 | ||
else{ | ||
if(T[loc].lchild != -1) | ||
loc = T[loc].lchild; | ||
else{ | ||
T[loc].lchild = pos, T[pos].father = loc; break; | ||
} | ||
} //向左到尽头并插入 | ||
}//从根结点开始搜索插入位置 | ||
} | ||
pos++; //完成插入,插入点后移 | ||
} | ||
void createBST(int N, BST T[]){ | ||
int num; | ||
for(int i = 0; i < N; i++){ | ||
cin>>num; | ||
Insert(T, num); | ||
} | ||
} | ||
|
||
int main(){ | ||
BST tree[MAXSIZE]; | ||
Initi(tree); | ||
int N; | ||
cin>>N; | ||
createBST(N, tree); | ||
for(int i = 0; i < N; i++){ | ||
if(i == 0){ | ||
cout<<-1<<endl; | ||
} | ||
else{ | ||
cout<<tree[tree[i].father].data<<endl; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
/****************** | ||
*一个改进思路:按顺序* | ||
*一次性将结点输入存入* | ||
*结构数组,再构建二叉* | ||
*排序树************* | ||
*******************/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include<iostream> | ||
using namespace std; | ||
|
||
typedef struct treeNode{ | ||
int num; | ||
treeNode *lchild, *rchild; | ||
}*BST; //不同于bst.cpp,此处采用链表结构 | ||
void Insert(BST &T, int n){ | ||
if(T == NULL){ | ||
treeNode *t = new treeNode; | ||
t->num = n; | ||
t->lchild = t->rchild = NULL; | ||
T = t; | ||
return; | ||
} | ||
if(n == T->num){ | ||
return; | ||
} | ||
else if(n > T->num){ | ||
Insert(T->rchild, n); | ||
} | ||
else{ | ||
Insert(T->lchild, n); | ||
} | ||
} //递归插入结点 | ||
BST CreateBST(int N){ | ||
BST T = NULL; | ||
int n; | ||
for(int i = 0; i < N; i++){ | ||
cin>>n; | ||
Insert(T, n); | ||
} | ||
return T; | ||
} | ||
void preOrder(BST T){ | ||
if(T == NULL){ | ||
return; | ||
} | ||
cout<<T->num<<' '; | ||
preOrder(T->lchild); | ||
preOrder(T->rchild); | ||
} //前序遍历 | ||
void inOrder(BST T){ | ||
if(T == NULL){ | ||
return; | ||
} | ||
inOrder(T->lchild); | ||
cout<<T->num<<' '; | ||
inOrder(T->rchild); | ||
} //中序遍历 | ||
void postOrder(BST T){ | ||
if(T == NULL){ | ||
return; | ||
} | ||
postOrder(T->lchild); | ||
postOrder(T->rchild); | ||
cout<<T->num<<' '; | ||
} //后序遍历 | ||
|
||
int main(){ | ||
int N; | ||
cin>>N; | ||
BST T = CreateBST(N); | ||
preOrder(T); | ||
cout<<endl; | ||
inOrder(T); | ||
cout<<endl; | ||
postOrder(T); | ||
delete T; | ||
return 0; | ||
} |
Oops, something went wrong.