北理2012年机考题

第一题
要求:
输入十个正整数数字 从小到大排序
输入
1,2,5,7,9,10,45,67,24,26
输出

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

#include <iostream>
#include<stdio.h>
using namespace std;

void BubbleSort(int *a,int size)
{
    int temp;
    for(int i=0;i<size;i++)
    for(int j=i+1;j<size;j++){
        if(a[i]>a[j]){
            temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    }
}

int main()
{
    int a[5] = {32,24,1,0,9};
    BubbleSort(a,5);
    for(int i=0;i<5;i++){
        printf("%d\t",a[i]);
    }
    return 0;
}

第二题
/*
要求:
学生有(学号,姓名,性别,年龄),初始化三个学生的信息
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)

#include<vector>
#include<iostream>
#include<string>
#include<algorithm>
#include<stdio.h>
using namespace std;

class student
{
    public:
    string name;
    string sex;
    string Sno;
    int age;

    void init(string nu,string na,string sx,int ag){
        Sno = nu;
        name = na;
        sex = sx;
        age = ag;
    }
};
    vector<student>st;

 bool cmp(student a ,student b)
 {
     return a.Sno.compare(b.Sno)<0;
 }

void print()
{
    sort(st.begin(),st.end(),cmp);
    for(int i=0;i<st.size();i++){
        printf("(%s,%s,%s,%d)",st[i].Sno.c_str(),st[i].name.c_str(),st[i].sex.c_str(),st[i].age);
    }
}

int main()
{
    student temp;
    temp.init("10","wes","f",23);
    st.push_back(temp);
    temp.init("20","ert","f",28);
    st.push_back(temp);
    temp.init("30","str","t",89);
    st.push_back(temp);

    string order;

    while(cin >> order){
        if(order[0]=='I'){
            order.erase(0,1);
            int pos = order.find(",");
            string nu = order.substr(0,pos);
            order.erase(0,pos+1);
            pos = order.find(",");
            string na = order.substr(0,pos);
            order.erase(0,pos+1);
            pos = order.find(",");
            string sx = order.substr(0,pos);
            order.erase(0,pos+1);
            int ag = atoi(order.c_str());
            temp.init(nu,na,sx,ag);
            st.push_back(temp);
            print();
        }
        else{
            order.erase(0,1);
            for(int i=0;i<st.size();i++){
                if(st[i].Sno==order){
                    st.erase(st.begin()+i,st.begin()+i+1);
                }
            }
            print();
        }
    }
    return 0;
}




第三题


利用后序和中序确定前序遍历的结果
输入(按后序,中序)
CHBEDA
CBHADE
输出
ABCHDE

#include<iostream>
#include<string>
using namespace std;
typedef struct node
{
    char data;
    struct node *lchild,*rchild;
}BTNode;

    //利用后序中序确定二叉树
void CreateRoot2(BTNode *&root,string post,string in)
{
    if(post.empty())
        return ;
    root=new BTNode();
    root->data=*(post.end()-1);
    root->lchild=root->rchild=NULL;
    string pl,pr,il,ir;
    int pos=in.find(*(post.end()-1));
    il=in.substr(0,pos);
    ir=in.substr(pos+1);
    pl=post.substr(0,pos);
    pr=post.substr(pos,ir.size());
    CreateRoot2(root->lchild,pl,il);
    CreateRoot2(root->rchild,pr,ir);
}

string re;
void pre(BTNode *root)
{
    if(root!=NULL)
    {
        re+=(*root).data;
        pre((*root).lchild);
        pre((*root).rchild);
    }
}
int main()
{
    BTNode *root;
    string post,in;
    while(cin>>post>>in)
    {
        re="";
        CreateRoot2(root,post,in);
        pre(root);
        cout<<re<<endl;
    }
    return 0;
}



1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 、 1资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READmE.文件(md如有),本项目仅用作交流学习参考,请切勿用于商业用途。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值