A 二叉排序树 - 文本输出
构造二叉排序树之后先序遍历输出,要注意如何处理特殊的输出格式。
#include <bits/stdc++.h>
using namespace std;
typedef struct {
int key;
int otherinfo;
}ElemType;
typedef struct BSTNode {
ElemType data;
struct BSTNode* lchild, * rchild;
}BSTNode,*BSTree;
void InsertBST(BSTree& T, ElemType e) {
if (!T) {
BSTNode* S = new BSTNode;
S->data = e;
S->lchild = S->rchild = NULL;
T = S;
}
else if (e.key < T->data.key)
InsertBST(T->lchild, e);
else if (e.key > T->data.key)
InsertBST(T->rchild, e);
}
void CreatBST(BSTree& T)
{
T = NULL;
ElemType e;
int n;
cin >> n;
while(n--){
cin >> e.key;
InsertBST(T, e);
}
}
string s="";
void InorderTree(BSTree T) {
cout<<s;
if (T)
{
cout << T->data.key << " ";
cout << endl;
if(T->lchild || T->rchild)
{
s+=" ";
InorderTree(T->lchild);
InorderTree(T->rchild);
for(int i=0;i<4;i++) s.pop_back();
}
}
else
{
cout<<"#"<<endl;
}
}
int main() {
BSTree T = NULL;
CreatBST(T);
InorderTree(T);
return 0;
}
B 销售排行榜
结构体把数据都储存进去,然后直接排序即可。第一遍按name排序把相同name的合并,第二遍按题意排序得到结果。
#include <bits/stdc++.h>
using namespace std;
struct shopg
{
string name;
long long num;
long long price;
}goods[100010];
bool pd1(shopg shopg1,shopg shopg2)
{
if(shopg1.name<shopg2.name) return true;
else return false;
}
bool pd2(shopg shopg1,shopg shopg2)
{
if(shopg1.num>shopg2.num) return true;
else if(shopg1.num==shopg2.num && shopg1.name<shopg2.name) return true;
else return false;
}
int main()
{
int i=0;
string s;
while(cin>>s)
{
goods[i].name=s;
cin>>goods[i].num;
cin>>goods[i].price;
goods[i].price*=goods[i].num;
cin>>s;
i++;
}
sort(goods,goods+i,pd1);
for(int j=0;j<i-1;j++)
{
if(goods[j].name==goods[j+1].name)
{
goods[j+1].num+=goods[j].num;
goods[j+1].price+=goods[j].price;
goods[j].num=0;
goods[j].price=0;
}
}
sort(goods,goods+i,pd2);
for(int j=0;j<i;j++)
{
if(goods[j].num!=0)
{
cout<<goods[j].name<<" "<<goods[j].num<<" "<<goods[j].price<<endl;
}
}
return 0;
}
C 二叉排序树-平衡因子
在A题的基础上多维护每个节点的左最大深度和右最大深度。
平衡因子为左最大深度-右最大深度。
#include <bits/stdc++.h>
using namespace std;
typedef struct {
int key;
int leftbal;
int rightbal;
}ElemType;
int cc;
typedef struct BSTNode {
ElemType data;
struct BSTNode* lchild, * rchild;
}BSTNode,*BSTree;
void InsertBST(BSTree& T, ElemType e) {
if (!T)
{
BSTNode* S = new BSTNode;
e.leftbal=cc;
e.rightbal=cc;
S->data = e;
S->lchild = S->rchild = NULL;
T = S;
}
else if (e.key < T->data.key)
{
cc++;
InsertBST(T->lchild, e);
T->data.leftbal=max(T->data.leftbal,cc);
}
else if (e.key > T->data.key)
{
cc++;
InsertBST(T->rchild, e);
T->data.rightbal=max(T->data.rightbal,cc);
}
}
void CreatBST(BSTree& T) {
T = NULL;
ElemType e;
int n;
cin >> n;
while(n--){
cin >> e.key;
e.leftbal=0;
e.rightbal=0;
cc=0;
InsertBST(T, e);
}
}
string s="";
void InorderTree(BSTree T) {
cout<<s;
if (T)
{
cout << T->data.key << "("<<T->data.leftbal-T->data.rightbal<<")";
cout << endl;
if(T->lchild || T->rchild)
{
s+=" ";
InorderTree(T->lchild);
InorderTree(T->rchild);
for(int i=0;i<4;i++) s.pop_back();
}
}
else
{
cout<<"#"<<endl;
}
}
int main() {
BSTree T = NULL;
CreatBST(T);
InorderTree(T);
return 0;
}
D 案例 1-1.1 二分查找
建议使用C++的二分查找函数lower_bound()
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,t;
cin>>n;
int a[n];
for(int i=0;i<n;i++) cin>>a[i];
cin>>t;
auto g=lower_bound(a,a+n,t);
if(*g!=t) cout<<-1<<endl;
else cout<<g-a+1<<endl;
return 0;
}
E 进阶实验 1-3.1:两个有序序列的中位数
不知道这题出在这一次作业的意义((((
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
int a[2*n];
for(int i=0;i<2*n;i++) cin>>a[i];
sort(a,a+2*n);
cout<<a[(2*n+1)/2-1]<<endl;
return 0;
}