zoukankan      html  css  js  c++  java
  • 北理工计算机复试上机 2012

    1.输入10个数,从小到大排序

    示例:

    输入:1,2,5,7,9,10,45,67,24,26

    输出:1,2,5,7,9,10,24,26,45,67

     1 #include<iostream>
     2 using namespace std;
     3 int main(){
     4 
     5     int a[10]={0};
     6     int x=0;
     7     while(x<10){
     8         cin>>a[x];
     9         x++;
    10     }
    11     
    12     for(int i=10;i>0;i--){
    13         int flag=0;
    14         for(int j=1;j<i;j++){
    15             if(a[j-1]>a[j]){
    16                 int temp=a[j-1];
    17                 a[j-1]=a[j];
    18                 a[j]=temp;
    19                 flag=1;
    20             }
    21         }//for
    22         if(flag==0)break;
    23     }
    24 
    25 
    26     for(int k=0;k<10;k++){
    27         cout<<a[k]<<" ";
    28     }
    29     cout<<endl;
    30 return 0;
      }

    2.学生有(学号,姓名,性别,年龄),初始化三个学生的信息
      (10,wes,f,23)(20,ert,f,45) (30,str,t,89),
    然后对学生信息进行插入和删除处理
    例如:
      I 12,rt,f,67
    表示插入
    12,rt,f,67
    D10
    表示删除学号为10的学生的信息
    每次操作完成以后输出所有学生的信息按学号从大到
    小排序
    输入:
    I 12,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 #include<iostream>
     2 #include<string>
     3 #include<algorithm>
     4 #include<vector>
     5 using namespace std;
     6 
     7 
     8 class Student{
     9 public:
    10     int id;
    11     string name;
    12     char sex;
    13     int age;
    14 };
    15 
    16 bool des(Student a,Student b){
    17 return a.id>b.id;
    18 };
    19  int main(){
    20     vector<Student> students;
    21     Student s;
    22     s.id=10;
    23     s.name="wes";
    24     s.sex='f';
    25     s.age=23;
    26     students.push_back(s);
    27 
    28     s.id=20;
    29     s.name="wes";
    30     s.sex='f';
    31     s.age=23;
    32     students.push_back(s);
    33 
    34     s.id=30;
    35     s.name="wes";
    36     s.sex='f';
    37     s.age=23;
    38     students.push_back(s);
    39     char order;
    40     cout<<"请输入命令"<<endl;
    41         vector<Student>::iterator i;
    42 
    43     while(cin>>order){
    44         if(order=='I'){
    45             cout<<"亲输入学生信息"<<endl;
    46             cin>>s.id>>s.name>>s.sex>>s.age;
    47             students.push_back(s);
    48             sort(students.begin(),students.end(),des);
    49             for(i=students.begin();i!=students.end();i++)
    50             {
    51                 cout<<(*i).id<<" "<<(*i).name<<" "<<(*i).sex<<" "<<(*i).age<<endl;
    52             }
    53         }
    54         if(order=='D'){
    55             cout<<"请输入删除学号"<<endl;
    56             int dnum;
    57             cin>>dnum;
    58             for(i=students.begin();i!=students.end();i++){
    59                 if((*i).id==dnum)
    60                 {
    61                     students.erase(i);
    62                     break;
    63                 }
    64             
    65             }
    66             sort(students.begin(),students.end(),des);
    67             for(i=students.begin();i!=students.end();i++)
    68             {
    69                 cout<<(*i).id<<" "<<(*i).name<<" "<<(*i).sex<<" "<<(*i).age<<endl;
    70             }
    71         }    
    72     }
    73 return 0;
    74 }

    3.利用后序和中序确定前序遍历结果
    示例:
      输入(按后序、中序):CHBEDA  CBHADE
      输出:ABCHDE    

      

     1 #include<iostream>
     2 #include<string>
     3 
     4 using namespace std;
     5 
     6 typedef struct TNode
     7 {
     8     char data;
     9     struct TNode *lchild,*rchild;
    10 
    11 }*Tree;
    12 
    13 void CreateTree(Tree &root,string in,string post);
    14 void PreOrder(Tree T);
    15 
    16 int main()
    17 {/**
    18 3.利用后序和中序确定前序遍历结果
    19 示例:
    20   输入(按后序、中序):CHBEDA  CBHADE
    21   输出:ABCHDE
    22 */
    23     string in,post;
    24     cout<<"请输入中序和后序遍历序列"<<endl;
    25     cin>>post>>in;
    26     Tree T;
    27     CreateTree(T,in,post);
    28     PreOrder(T);
    29     cout<<endl;
    30     return 0;
    31 }
    32 void PreOrder(Tree T)
    33 {
    34     if(T!=NULL)
    35     {
    36         cout<<T->data;
    37         PreOrder(T->lchild);
    38         PreOrder(T->rchild);
    39     }
    40 
    41 }
    42 
    43 void CreateTree(Tree &root,string in,string post)
    44 {
    45     if(in.length()==0)return;
    46 
    47         root=new TNode();
    48         root->data=post[post.length()-1];
    49         root->lchild=root->rchild=NULL;
    50 
    51         string ina,inb,posta,postb;
    52         int pos=in.find(post[post.length()-1]);//找到根节点在中序便利中的位置
    53         ina=in.substr(0,pos);//截取从零开始的pos个字符  就是左子树的中序便利序列
    54         inb=in.substr(pos+1);//截取pos后的所有字符      就是右子树的中序便利序列
    55         posta=post.substr(0,ina.length());//截取后续便利的前一段      就是左子树的后续便利序列,和左子树中序遍历序列个数相同
    56         postb=post.substr(ina.length(),inb.length());
    57 
    58         CreateTree(root->lchild,ina,posta);
    59         CreateTree(root->rchild,inb,postb);
    60 
    61 
    62 }
  • 相关阅读:
    make_shared和shared_ptr的区别
    bitcoined
    ofstream的使用方法--超级精细。C++文件写入、读出函数(转)
    visual studio 安装过程
    Socket通信原理探讨(C++为例)
    以太坊的记录和关注点
    以太坊的应用
    以太坊
    比特币、以太坊
    c++
  • 原文地址:https://www.cnblogs.com/PPWEI/p/8453619.html
Copyright © 2011-2022 走看看