华科部分复试机试题目

1、

输入一系列整数,建立二叉排序树,并进行前序,中序,后序遍历。

输入描述:

输入第一行包括一个整数n(1<=n<=100)。
接下来的一行包括n个整数。

输出描述:

可能有多组测试数据,对于每组数据,将题目所给数据建立一个二叉排序树,并对二叉排序树进行前序、中序和后序遍历。
每种遍历结果输出一行。每行最后一个数据之后有一个空格。

输入中可能有重复元素,但是输出的二叉树遍历序列中重复元素不用输出。

示例1

输入

5
1 6 5 9 8

输出

1 6 5 9 8 
1 5 6 8 9 
5 8 9 6 1 
#include<iostream>
#include<malloc.h>
using namespace std;
typedef struct node
{
    int data;
    struct node *Lchild;
    struct node *Rchild;

}BSTNode;
//insert
int BSTinsert(BSTNode* &p,int key)
{
    if(p==NULL)
    {
        p=(BSTNode *)malloc(sizeof(BSTNode));
        p->data=key;
        p->Lchild=p->Rchild=NULL;
        return 1;
    }
    else if(key==p->data)
    {
        return 0;
    }
    else if(key < p->data)
    {
        BSTinsert(p->Lchild,key);
    }
    else
    {
        BSTinsert(p->Rchild,key);
    }
    return 0;
}
//build
void BSTcreate(BSTNode* &T,int num[],int n)
{
    T=NULL;
    int i=0;
    while(i<n)
    {
        BSTinsert(T,num[i]);
        i++;
    }
}
//preorder
void preOrder(BSTNode *T)
{
    if(T!=NULL)
    {
        cout<<T->data<<" ";
        preOrder(T->Lchild);
        preOrder(T->Rchild);
    }
}
//inorder
void inOrder(BSTNode *T)
{
    if(T!=NULL)
    {
        inOrder(T->Lchild);
        cout<<T->data<<" ";
        inOrder(T->Rchild);
    }
}
//afterorder
void afterOrder(BSTNode *T)
{
    if(T!=NULL)
    {
        afterOrder(T->Lchild);
        afterOrder(T->Rchild);
        cout<<T->data<<" ";
    }
}
int main()
{
    int n,num[100];
    BSTNode *T;
    while(cin>>n)
    {

    for(int i=0;i<n;i++)
    {
        cin>>num[i];
    }
    BSTcreate(T,num,n);
    preOrder(T);
    cout<<endl;
    inOrder(T);
    cout<<endl;
    afterOrder(T);
    cout<<endl;
    }
    return 0;
}

2、

题目描述

二叉排序树,也称为二叉查找树。可以是一颗空树,也可以是一颗具有如下特性的非空二叉树: 1. 若左子树非空,则左子树上所有节点关键字值均不大于根节点的关键字值; 2. 若右子树非空,则右子树上所有节点关键字值均不小于根节点的关键字值; 3. 左、右子树本身也是一颗二叉排序树。 现在给你N个关键字值各不相同的节点,要求你按顺序插入一个初始为空树的二叉排序树中,每次插入后成功后,求相应的父亲节点的关键字值,如果没有父亲节点,则输出-1。

输入描述:

输入包含多组测试数据,每组测试数据两行。
第一行,一个数字N(N<=100),表示待插入的节点数。
第二行,N个互不相同的正整数,表示要顺序插入节点的关键字值,这些值不超过10^8。

输出描述:

输出共N行,每次插入节点后,该节点对应的父亲节点的关键字值。

示例1

输入

5
2 5 1 3 4

输出

-1
2
2
5
3
#include<iostream>
#include<malloc.h>
using namespace std;
typedef struct node
{
    int data;
    struct node *Lchild;
    struct node *Rchild;

}BSTNode;
//insert
void BSTinsert(BSTNode* &p,int key,BSTNode* &pre,int &x)
{
    //BSTNode *pre=p;
    if(p==NULL&&pre==NULL)
    {
        p=(BSTNode *)malloc(sizeof(BSTNode));
        p->data=key;
        p->Lchild=p->Rchild=NULL;
        pre=p;
        x=-1;
    }
    else if(p==NULL&&pre!=NULL)
    {
        p=(BSTNode *)malloc(sizeof(BSTNode));
        p->data=key;
        p->Lchild=p->Rchild=NULL;
        x=pre->data;
    }
    else if(key==p->data)
    {
        ;
    }
    else if(key<p->data&&pre!=NULL)
    {
        BSTinsert(p->Lchild,key,p,x);
    }
    else
    {
        BSTinsert(p->Rchild,key,p,x);
    }
}
//build
void BSTcreate(BSTNode* &T,int num[],int res[],int n)
{
    T=NULL;
    BSTNode *pre=NULL;
    int i=0;
    while(i<n)
    {
        BSTinsert(T,num[i],pre,res[i]);
        i++;
    }
}
/*
//preorder
void preOrder(BSTNode *T)
{
    if(T!=NULL)
    {
        cout<<T->data<<" ";
        preOrder(T->Lchild);
        preOrder(T->Rchild);
    }
}
//inorder
void inOrder(BSTNode *T)
{
    if(T!=NULL)
    {
        inOrder(T->Lchild);
        cout<<T->data<<" ";
        inOrder(T->Rchild);
    }
}
//afterorder
void afterOrder(BSTNode *T)
{
    if(T!=NULL)
    {
        afterOrder(T->Lchild);
        afterOrder(T->Rchild);
        cout<<T->data<<" ";
    }
}
*/
int main()
{
    int n,num[100],res[100];
    BSTNode *T;
    while(cin>>n)
    {

    for(int i=0;i<n;i++)
    {
        cin>>num[i];
    }
    BSTcreate(T,num,res,n);
    for(int i=0;i<n;i++)
    {
        cout<<res[i]<<endl;
    }
    }
    return 0;
}

3、

题目描述

对N个长度最长可达到1000的数进行排序。

输入描述:

输入第一行为一个整数N,(1<=N<=100)。
接下来的N行每行有一个数,数的长度范围为1<=len<=1000。
每个数都是一个正数,并且保证不包含前缀零。

输出描述:

可能有多组测试数据,对于每组数据,将给出的N个数从小到大进行排序,输出排序后的结果,每个数占一行。

示例1

输入

3
11111111111111111111111111111
2222222222222222222222222222222222
33333333

输出

33333333
11111111111111111111111111111
2222222222222222222222222222222222
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int equalxy(string x,string y)
{
    unsigned long k;
    if(x.length()>y.length())
    {
        return 1;
    }
    else if(x.length()<y.length())
    {
        return -1;
    }
    else
    {
        for(k=0;k<x.length();k++)
        {
            if(x[k]>y[k])
            {
                return 1;
            }
            else if(x[k]<y[k])
            {
                return -1;
            }
        }
        if(k==x.length())
            return 0;
    }
    return 0;
}
int main()
{
    int n;
    string str;
    vector<string> st;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>str;
        st.push_back(str);
    }
    for(int i=0;i<n;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            if(1==equalxy(st[i],st[j]))
            {
                string temp=st[i];
                st[i]=st[j];
                st[j]=temp;
            }
        }
    }
    for(int i=0;i<n;i++)
        cout<<st[i]<<endl;
    return 0;
}

4、

题目描述

N阶楼梯上楼问题:一次可以走两阶或一阶,问有多少种上楼方式。(要求采用非递归)

输入描述:

输入包括一个整数N,(1<=N<90)。

输出描述:

可能有多组测试数据,对于每组数据,
输出当楼梯阶数是N时的上楼方式个数。

示例1

输入

4

输出

5
#include <iostream>
using namespace std;
int fun(int n)
{
    int a[100];
    a[1]=1;
    a[2]=2;
    for(int i=3;i<=n;i++)
    {
        a[i]=a[i-2]+a[i-1];
    }
    return a[n];
}
int main()
{
    int n;
    cin>>n;
    cout<<fun(n);
    return 0;
}

5、

题目描述

对给定的一个字符串,找出有重复的字符,并给出其位置,如:abcaaAB12ab12 输出:a,1;a,4;a,5;a,10,b,2;b,11,1,8;1,12, 2,9;2,13。

输入描述:

输入包括一个由字母和数字组成的字符串,其长度不超过100。

输出描述:

可能有多组测试数据,对于每组数据,
按照样例输出的格式将字符出现的位置标出。

1、下标从0开始。
2、相同的字母在一行表示出其出现过的位置。

示例1

输入

abcaaAB12ab12

输出

a:0,a:3,a:4,a:9
b:1,b:10
1:7,1:11
2:8,2:12
#include<iostream>
#include<cstring>
#include<vector>
#include<memory>
using namespace std;
int main()
{
    string str,str1;
    int k=0,counter=0;
    unsigned long  path[20];
    cin>>str;
    str1=str;
    for(unsigned long i=0;i<str.length();i++)
    {
        if(str[i]=='_')
            continue;
        counter=1;
        path[0]=i;
        k=1;
        for(unsigned long j=i+1;j<str.length();j++)
        {
            if(str[i]==str[j]&&str[i]!='_'&&str[j]!='_')
            {
                counter++;
                path[k]=j;
                k++;
                str[j]='_';
            }
        }
        //cout<<"str===="<<str<<endl;
        //cout<<"k===="<<k<<",  counter==="<<counter<<endl;
        if(counter>=2)
        {
            for(int x=0;x<k;x++)
            {
                if(x==k-1)
                    cout<<str1[i]<<":"<<path[x]<<endl;
                else cout<<str1[i]<<":"<<path[x]<<",";
            }
        }
        memset(path,0,20);
    }
    return 0;
}

6、

题目描述

输入一个整数,将其转换成八进制数输出。

输入描述:

输入包括一个整数N(0<=N<=100000)。

输出描述:

可能有多组测试数据,对于每组数据,
输出N的八进制表示数。

示例1

输入

7
8
9

输出

7
10
11
#include<iostream>
using namespace std;
int main()
{
    int n,sum=0,i=1,a,b;
    while(cin>>n){
        sum=0;
        i=1;
        if(n==0)
        {
            cout<<0<<endl;
            break;
        }
        while(n>0){
        a=n/8;
        b=n%8;
        sum=sum+b*i;
        n=a;
        i=i*10;
        }
        cout<<sum<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值