码迷,mamicode.com
首页 > 其他好文 > 详细

北理工计算机复试上机 2012

时间:2018-02-19 11:57:39      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:order   temp   前序遍历   using   student   计算机   bsp   i++   span   

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    

  

 

北理工计算机复试上机 2012

标签:order   temp   前序遍历   using   student   计算机   bsp   i++   span   

原文地址:https://www.cnblogs.com/PPWEI/p/8453619.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!