第一题
要求:
输入十个正整数数字 从小到大排序
输入
1,2,5,7,9,10,45,67,24,26
输出
1,2,5,7,9,10,24,26,45,67
#include <iostream>
#include<stdio.h>
using namespace std;
void BubbleSort(int *a,int size)
{
int temp;
for(int i=0;i<size;i++)
for(int j=i+1;j<size;j++){
if(a[i]>a[j]){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
int main()
{
int a[5] = {32,24,1,0,9};
BubbleSort(a,5);
for(int i=0;i<5;i++){
printf("%d\t",a[i]);
}
return 0;
}
第二题
/*
要求:
学生有(学号,姓名,性别,年龄),初始化三个学生的信息
(10,wes,f,23)(20,ert,f,45)(30,str,t,89),然后对学生信息进行插入和删除处理
例如
I12,rt,f,67表示插入12,rt,f,67
D10 表示删除学号为10的学生的信息
每次操作完成以后输出所有学生的信息按学号从大到小排序
输入:
I12,rt,f,67
输出
(10,wes,f,23),(12,rt,f,67),(20,ert,f,45),(30,str,t,89)
输入:
D10
输出
(12,rt,f,67),(20,ert,f,45),(30,str,t,89)
#include<vector>
#include<iostream>
#include<string>
#include<algorithm>
#include<stdio.h>
using namespace std;
class student
{
public:
string name;
string sex;
string Sno;
int age;
void init(string nu,string na,string sx,int ag){
Sno = nu;
name = na;
sex = sx;
age = ag;
}
};
vector<student>st;
bool cmp(student a ,student b)
{
return a.Sno.compare(b.Sno)<0;
}
void print()
{
sort(st.begin(),st.end(),cmp);
for(int i=0;i<st.size();i++){
printf("(%s,%s,%s,%d)",st[i].Sno.c_str(),st[i].name.c_str(),st[i].sex.c_str(),st[i].age);
}
}
int main()
{
student temp;
temp.init("10","wes","f",23);
st.push_back(temp);
temp.init("20","ert","f",28);
st.push_back(temp);
temp.init("30","str","t",89);
st.push_back(temp);
string order;
while(cin >> order){
if(order[0]=='I'){
order.erase(0,1);
int pos = order.find(",");
string nu = order.substr(0,pos);
order.erase(0,pos+1);
pos = order.find(",");
string na = order.substr(0,pos);
order.erase(0,pos+1);
pos = order.find(",");
string sx = order.substr(0,pos);
order.erase(0,pos+1);
int ag = atoi(order.c_str());
temp.init(nu,na,sx,ag);
st.push_back(temp);
print();
}
else{
order.erase(0,1);
for(int i=0;i<st.size();i++){
if(st[i].Sno==order){
st.erase(st.begin()+i,st.begin()+i+1);
}
}
print();
}
}
return 0;
}
第三题
利用后序和中序确定前序遍历的结果
输入(按后序,中序)
CHBEDA
CBHADE
输出
ABCHDE
#include<iostream>
#include<string>
using namespace std;
typedef struct node
{
char data;
struct node *lchild,*rchild;
}BTNode;
//利用后序中序确定二叉树
void CreateRoot2(BTNode *&root,string post,string in)
{
if(post.empty())
return ;
root=new BTNode();
root->data=*(post.end()-1);
root->lchild=root->rchild=NULL;
string pl,pr,il,ir;
int pos=in.find(*(post.end()-1));
il=in.substr(0,pos);
ir=in.substr(pos+1);
pl=post.substr(0,pos);
pr=post.substr(pos,ir.size());
CreateRoot2(root->lchild,pl,il);
CreateRoot2(root->rchild,pr,ir);
}
string re;
void pre(BTNode *root)
{
if(root!=NULL)
{
re+=(*root).data;
pre((*root).lchild);
pre((*root).rchild);
}
}
int main()
{
BTNode *root;
string post,in;
while(cin>>post>>in)
{
re="";
CreateRoot2(root,post,in);
pre(root);
cout<<re<<endl;
}
return 0;
}