2013年北理复试上机题

1、求两个数的最大公约数。输入:24,18 输出:6。

#include<iostream>
using namespace std;

int gcd(int a, int b)
{
    if (b == 0)return a;
    else gcd(b, a%b);
}

int main() 
{
    int a, b;
    cin >> a >> b;
    cout << gcd(a, b) << endl;
    return 0;
}

2、输入一组英文单词,按字典顺序(大写与小写字母具有相同表示)排序输出

输入:Information Info Inform info Suite suite suit

输出:Info info Inform Information suit Suite suite

#include<iostream>
#include<string>
#include<algorithm>
#include<cctype>
using namespace std;

struct Node
{
    string a, c;
}t[100];

bool cmp(Node x, Node y)
{
    return x.a < y.a;
}

int main() 
{
    string s;
    int loc = 0;
    while (cin >> s)t[loc++].a = s;
    for (int i = 0; i < loc; i++)
    {
        t[i].c = t[i].a;
        for (int j = 0; j < t[i].a.size(); j++)t[i].a[j] = tolower(t[i].a[j]);
    }
    sort(t, t + loc, cmp);
    for (int i = 0; i < loc; i++)
    {
        if (i == 0)cout << t[i].c;
        else cout << " " << t[i].c;
    }
    cout << endl;
    return 0;
}

3、输入表达式,输出相应二叉树的先序遍历结果

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

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

#include<cstdio>
#include<cstring>
#include<iostream>
#include<stack>
using namespace std;

struct Node
{
    Node* l;
    Node* r;
    char c;
}t[50];

int loc = 0;

Node* create()
{
    t[loc].l = t[loc].r = NULL;
    return &t[loc++];
}

void preorder(Node* t)
{
    cout << t->c;
    if (t->l != NULL)preorder(t->l);
    if (t->r != NULL)preorder(t->r);
}

int priority(char c)
{
    if (c == '+' || c == '-')return 0;
    else if (c == '*' || c == '/')return 1;
}

int main()
{
    char in[205];
    char post[205] = { 0 };
    stack<char> s;
    scanf("%s", in);
    int l = strlen(in);
    int size = 0;
    for (int i = 0; i < l; i++)
    {
        if (in[i] >= 'a'&&in[i] <= 'z')post[size++] = in[i];
        else if (in[i] == '(')s.push(in[i]);
        else if (in[i] == ')')
        {
            if (s.empty())
            {
                printf("Wrong!\n");
                return 0;
            }
            while (s.top() != '(')
            {
                post[size++] = s.top();
                s.pop();
            }
            if (s.top() != '(')printf("Wrong!/n");
            else s.pop();//弹出开括号
        }
        else if (in[i] == '*' || in[i] == '/' || in[i] == '+' || in[i] == '-')
        {
            while (!s.empty() && priority(s.top()) >= priority(in[i]) && s.top() != '(')
            {
                post[size++] = s.top();
                s.pop();
            }
            s.push(in[i]);
        }
    }
    while(!s.empty())
    {
        post[size++] = s.top();
        s.pop();
    }
    stack<Node*>tr;
    for (int i = 0; i < size; i++)
    {
        Node* n = create();
        n->c = post[i];
        if (n->c >= 'a'&&n->c <= 'z')tr.push(n);
        else if (n->c == '*' || n->c == '/' || n->c == '+' || n->c == '-')
        {
            Node* a = tr.top(); tr.pop();
            Node* b = tr.top(); tr.pop();
            n->r = a; n->l = b;
            tr.push(n);
        }
    }
    Node* root = tr.top();
    preorder(root);
    cout << endl;
    return 0;
}

 

posted @ 2019-08-19 16:16  郭怡柔  阅读(209)  评论(0)    收藏  举报