北理工计算机复试上机 2013

1. 求两个数的最大公约数(似乎有个辗转相除法,为什么不用呢,没错,我不会)

  示例:

    输入:24,18

    输出:6

 1 // 2013_1.cpp : Defines the entry point for the console application.
 2 //
 3 #include<iostream>
 4 using namespace std;
 5 
 6 int main(int argc, char* argv[])
 7 {
 8     int a,b;
 9     cout<<"输入:";
10     cin>>a>>b;
11     int max=a>b?b:a;//返回两个数中最小的那个
12     int result=0;
13     for(int i=1;i<=max;++i){
14         if(a%i==0&&b%i==0){
15             result=i;
16         }
17     }
18 cout<<"输出:"<<result<<endl;
19     return 0;
20 }

2.输入一组英文单词,按字典顺序(不区分大小写)排序输出。(这个是在网上抄的,不会。。)

  示例:

    输入:Information Info Inform info Suite suite suit

    输出:Info info Inform Information suit Suite suite

 1 // 2013_2.cpp : Defines the entry point for the console application.
 2 //
 3 
 4 #include <iostream>
 5 #include<algorithm>
 6 #include<vector>
 7 #include<string>
 8 using namespace std;
 9 
10 bool de(string a ,string b){
11     if(a[0]>='A'&&a[0]<='Z')
12         a[0]=a[0]+32;
13     if(b[0]>='A'&&b[0]<='Z')
14         b[0]=b[0]+32;
15     return (a<b);
16 }
17 
18 int main(int argc, char* argv[])
19 {
20     vector<string> str;
21     string s;
22     while(1){
23         cout<<"请输入,00结束"<<endl;
24         while(cin>>s){
25             if(s=="00")break;
26             str.push_back(s);
27         }
28         sort(str.begin(),str.end(),de);
29         vector<string>::iterator i;
30         for(i=str.begin();i!=str.end();i++)
31             cout<<*i<<" ";
32         cout<<endl;
33     }
34     return 0;
35 }

3. 输入表达式,输出表达式先序遍历结果(没错,又是抄的)

  示例:

    输入:a+b*(c-d)-e/f

    输出:-+a*b-cd/ef

  1 #include<iostream>
  2 #include<stack>
  3 #include<string>
  4 using namespace std;
  5 typedef struct no
  6 {
  7     char data;
  8     struct no *lchild,*rchild;
  9 }*node;
 10 int getpr(char a)
 11 {
 12     if(a=='+'||a=='-')return 1;
 13     if(a=='*'||a=='/')return 2;
 14     if(a=='('||a==')')return 0;
 15 }//优先级
 16 string res(string in)
 17 {
 18     stack<char> st;
 19     string post="";
 20     for(int i=0; i<in.length(); i++)
 21     {
 22         if(in[i]=='+'||in[i]=='-'||in[i]=='*'||in[i]=='/')
 23         {
 24             if(!st.empty())
 25             {
 26                 if(getpr(in[i])>getpr(st.top()))
 27                     st.push(in[i]);
 28                 else
 29                 {
 30                     while(getpr(in[i])<=getpr(st.top()))
 31                     {
 32                         if(getpr(in[i])>getpr(st.top())) break;
 33                         post+=st.top();
 34                         st.pop();
 35                         if(st.empty()) break;
 36                     }
 37                     st.push(in[i]);
 38                 }//栈不空
 39             }//if 是操作符不空
 40 
 41             if(st.empty()) st.push(in[i]);
 42         }//if 是操作符
 43 
 44         if(in[i]=='(') st.push(in[i]);
 45         if(in[i]==')')
 46         {
 47             while(st.top()!='(')
 48             {
 49                 if(st.top()=='(')break;    //是)
 50                 post+=st.top();
 51                 st.pop();
 52             }
 53             st.pop();
 54         }
 55         if(in[i]!='+'&&in[i]!='-'&&in[i]!='/'&&in[i]!='*'&&in[i]!='('&&in[i]!=')')
 56             post+=in[i];
 57     }// for(int i=0;i<in.length();i++)
 58     while(!st.empty())
 59     {
 60         post+=st.top();
 61         st.pop();
 62     }
 63     return post;
 64 }//res
 65 node create(string sa)
 66 {
 67     node ss;
 68     stack<node>st;
 69     for(int i=0; i<sa.length(); i++)
 70     {
 71         if(sa[i]!='+'&&sa[i]!='-'&&sa[i]!='/'&&sa[i]!='*')
 72         {
 73             ss=new no(),ss->data=sa[i];
 74             ss->lchild=ss->rchild=NULL;
 75             st.push(ss);
 76         }
 77         else
 78         {
 79             ss=new no(),ss->data=sa[i];
 80             ss->rchild=st.top();
 81             st.pop();
 82             ss->lchild=st.top();
 83             st.pop();
 84             st.push(ss);
 85         }
 86     }//for
 87     return st.top();
 88 }
 89 void pre(node sa)
 90 {
 91     if(sa!=NULL)
 92     {
 93         cout<<sa->data;
 94         pre(sa->lchild);
 95         pre(sa->rchild);
 96     }
 97 }
 98 main()
 99 {
100     cout<<"请输入中缀表达式:"<<endl;
101     string in,post;
102     node head;
103     head=new no();
104     cin>>in;
105     cout<<"转换为后缀表达式为:"<<endl;
106     post=res(in);
107     cout<<post<<endl;
108     cout<<"构建表达式树......"<<endl;
109     head=create(post);
110     cout<<"这颗表达式树的前序(前缀表达式)为: "<<endl;
111     pre(head);
112     cout<<endl;
113 }
原文地址:https://www.cnblogs.com/PPWEI/p/8452727.html