北理工计算机复试上机 2014

本人也是练习,如果有错误,欢迎指正weishuangjian2018@163.com,也可留言

1. 系统中有最近打开文件的记录,现用整数表示打开的文件名,且显示最近3个打开的文件,输出文件序列。

  示例:

    输入:1  输出:1

    输入:2  输出:2,1

    输入:3  输出:3,2,1 

    输入:4  输出:4,3,2

    输入:1  输出:1,4,3

    输入:4  输出:1,4,3

    输入:3  输出:1,4,3

 1 // 2014_1.cpp : Defines the entry point for the console application.
 2 //
 3 
 4 #include<iostream>
 5 using namespace std;
 6 
 7 char a[3]={'0','0','0'};
 8 char c='z';
 9 int pos=2;
10 
11 void q_in(){
12     int flag=0;
13     for(int i=0;i<3;i++){
14         if(a[i]==c)flag=1;
15     }
16     if(flag==0){
17         for(int i=0;i<2;i++)a[i]=a[i+1];
18         a[2]=c;
19     }
20 }
21 void q_out(){
22     for(int i=2;i>=0;i--)
23         if(a[i]!='0'){
24             if(i!=2)cout<<",";
25             cout<<a[i];
26         }
27 }
28 
29 
30 int main(int argc, char* argv[])
31 {
32     
33     
34     cout<<"输入:";
35     cin>>c;
36     while(c!='0'){
37         q_in();
38         cout<<"输出:";
39         q_out();
40         cout<<"
输入:";
41         cin>>c;
42     }
43     return 0;
44 }

2.在第一题基础上,稍作改动,显示最新打开的文件(红色为修改代码)

示例:

    输入:1  输出:1

    输入:2  输出:2,1

    输入:3  输出:3,2,1 

    输入:4  输出:4,3,2

    输入:1  输出:1,4,3

    输入:4  输出:4,1,3

    输入:3  输出:3,4,1

 1 // 2014_2.cpp : Defines the entry point for the console application.
 2 //
 3 
 4 #include<iostream>
 5 using namespace std;
 6 
 7 char a[3]={'0','0','0'};
 8 char c='z';
 9 int pos=2;
10 
11 void q_in(){
12     int flag=-1;
13     for(int i=0;i<3;i++){
14         if(a[i]==c)flag=i;
15     }
16     if(flag==-1){
17         for(int i=0;i<2;i++)a[i]=a[i+1];
18         a[2]=c;
19     }else{//flag!=-1
20         cout<<"flag="<<flag<<endl;
21         for(int j=flag;j<2;++j)a[j]=a[j+1];
22         a[2]=c;
23 
24     }
25 }
26 void q_out(){
27     for(int i=2;i>=0;i--)
28         if(a[i]!='0'){
29             if(i!=2)cout<<",";
30             cout<<a[i];
31         }
32 }
33 
34 int main()
35 {
36     cout<<"输入:";
37     cin>>c;
38     while(c!='0'){
39         q_in();
40         cout<<"输出:";
41         q_out();
42         cout<<"
输入:";
43         cin>>c;
44     }
45     return 0;
46 }

3. 求广义表深度,示例:

  输入:(c,((d,e),f),h)

  输出:3

 1 // 2014_3.cpp : Defines the entry point for the console application.
 2 //
 3 #include<iostream>
 4 #include<string>
 5 using namespace std;
 6 int main(int argc, char* argv[])
 7 {
 8     cout<<"请输入广义表:";
 9     string str;
10     cin>>str;
11     int dep=0;
12     int max=0;
13     for(int i=0;i<str.length();i++){
14         if(str[i]=='(')dep++;
15         if(str[i]==')')dep--;
16         if(dep>max)max=dep;
17     }
18     cout<<"
广义表深度:"<<max<<endl;
19     return 0;
20 }
原文地址:https://www.cnblogs.com/PPWEI/p/8452651.html