2012年北理复试上机题

1、输入十个正整数数字,从小到大排序。

输入

1,2,5,7,9,10,45,67,24,26

输出

1,2,5,7,9,10,24,26,45,67

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


int main() 
{
    int a[12];
    for (int i = 0; i < 10; i++)
    {
        cin >> a[i];
        getchar();
    }
    sort(a, a + 10);
    for (int i = 0; i < 9; i++)cout << a[i] << ",";
    cout << a[9] <<endl;
    return 0;
}

2、学生有(学号,姓名,性别,年龄),初始化三个学生的信息(10,wes,f,23),(20,ert,f,45),(30,str,t,89),然后对学生信息进行插入和删除处理。例如:I12,rt,f,67表示插入(12,rt,f,67),D10 表示删除学号为10的学生的信息。每次操作完成以后输出所有学生的信息按学号从大到小排序。

输入:I12,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)

 sscanf使用链接(真香)

#include<iostream>
#include<string>
#include<vector>
#include<cstdio>
#include<algorithm>
using namespace std;

class Student
{
public:
    int id;
    char name[45];
    char sex;
    int age;
    Student(int i, const char s[], char c, int a) :id(i), sex(c), age(a) { strcpy(name, s); }
    Student(){}
    void show()
    {
        cout << "(" << id << "," << name << "," << sex << "," << age << ")";
    }
};

vector<Student> v;
vector<Student>::iterator i;

bool cmp(Student &a, Student &b)
{
    return a.id < b.id;
}

void print()
{
    sort(v.begin(), v.end(), cmp);
    for (i = v.begin(); i != v.end(); i++)
    {
        if (i == v.begin())(*i).show();
        else
        {
            cout << ",";
            (*i).show();
        }
    }
    cout << endl;
}

bool del(int idx)
{
    for (i = v.begin(); i != v.end(); i++)
    {
        if ((*i).id == idx)
        {
            v.erase(i);
            return true;
        }
    }
    return false;
}

int main() 
{
    Student s1(10, "wes", 'f', 23);
    v.push_back(s1);
    Student s2(20, "ert", 'f', 45);
    v.push_back(s2);
    Student s3(30, "str", 't', 89);
    v.push_back(s3);
    cout << "请输入指令,Ctrl+Z停止:" << endl;
    char o[105];
    Student s;
    while (cin >> o)
    {
        if (o[0] == 'I')
        {
            sscanf(o, "I%d,%[^,],%c,%d", &s.id, s.name, &s.sex, &s.age);
            v.push_back(s);
            print();
        }
        else if (o[0] == 'D')
        {
            int idx;
            sscanf(o, "D%d", &idx);
            if (del(idx))print();
            else cout << "不存在该学号的学生!" << endl;
        }
        else cout << "错误指令!" << endl;
    }
    return 0;
}

3、利用后序和中序确定前序遍历的结果

输入(后序,中序):CHBEDA,CBHADE     

输出:ABCHDE

#include<iostream>
#include<string>
using namespace std;
int loc = 0;
string s1, s2;

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

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

Node* build(int b1,int e1,int b2,int e2)
{
    Node* root = create();
    char c = s1[e1];
    root->c = c;
    int idx = b2;
    while (c != s2[idx])idx++;
    if (b2 != idx)root->l = build(b1, b1 + idx - b2 - 1, b2, idx - 1);
    if (e2 != idx)root->r = build(e1 - e2 + idx, e1 - 1, idx + 1, e2);
    return root;
}

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

int main() 
{
    cin >> s1 >> s2;
    Node * root = build(0, s1.size() - 1, 0, s2.size() - 1);
    preorder(root);
    cout << endl;
    return 0;
}

 

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