/*
第一题
要求:
输入十个正整数数字 从小到大排序
输入
1,2,5,7,9,10,45,67,24,26
输出
1,2,5,7,9,10,24,26,45,67
*/
#include<iostream> #include<algorithm> using namespace std; int main() { int t; int st[100]; char c; for(int i=0;i<9;i++) { cin>>st[i]>>c; } cin>>st[9]; sort(st,st+10); for(i=0;i<9;i++) cout<<st[i]<<","; cout<<st[9]<<endl; 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)
输入
1,2,5,7,9,10,45,67,24,26
输出
1,2,5,7,9,10,24,26,45,67
*/
#include<vector> #include<iostream> #include<string> #include<cmath> #include<sstream> #include<algorithm> using namespace std; class student { public: string num; string name; string sex; int age; void init(string nu,string na,string se,int ag) { num=nu; name=na; sex=se; age=ag; } }; bool cmp(student a,student b) { return a.num.compare(b.num)<0; } vector<student>st; void print() { sort(st.begin(),st.end(),cmp); for(int i=0;i<(st.size()-1);i++) cout<<"("<<st[i].num<<","<<st[i].name<<","<<st[i].sex<<","<<st[i].age<<"),"; cout<<"("<<st[st.size()-1].num<<","<<st[st.size()-1].name<<","<<st[st.size()-1].sex<<","<<st[st.size()-1].age<<")"<<endl; } int main() { /* (10,wes,f,23)(20,ert,f,45)(30,str,t,89) */ student temp; temp.init("10","wes","f",23); st.push_back(temp); temp.init("20","ert","f",45); 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 se=order.substr(0,pos); order.erase(0,pos+1); int ag=atoi(order.c_str()); temp.init(nu,na,se,ag); st.push_back(temp); print(); } else { order.erase(0,1); for(int i=0;i<st.size();i++) if(st[i].num==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; }
最后只做出两道,第三道用递归一直调试不出来吗,郁闷啊