您的位置:首页 > 其它

创新工场笔试题--笔试地点:华南理工大学

2009-10-21 17:10 239 查看
#include<iostream>
#include<string>
#include<math.h>
using namespace std;
struct node
{
int key;
node *left;
node *right;
};
node* buildTree(node* head,int value);
void showTree(node* head);
int main()
{
/*
//第一题:将输入的一句英文句子按单词逆序输出。例如:
//输入:I love python
//输出:python love I
string s[1000];
int size = 0;
string t;
while(cin>>t)
{
s[size] = t;
size++;
}
for(int i = size;i >= 0;--i)
cout<<s[i]<<" ";
cout<<endl;
*/
/*
//第二题:随便输入一个正整数,输出比它大的最小质数。例如:
//输入:9
//输出:11
int num = 0;
cin>>num;
while(1)
{
++num;
int mark = 0;
for(int i = sqrt((float)num);i >= 2;--i)
if(num % i == 0)
{
mark = 1;
break;
}
if(0 == mark)
{
cout<<num<<endl;
break;
}
else
continue;
}
*/
//第三题:任意给定一个整形数组,基于数组建立一棵二叉排序树,要求左子树所有节点比根小,右子树所有节点比根大。
int arr[10] = {16,14,18,10,12,20,9,17,15,19};
for(int j = 0;j != 10;++j)
cout<<arr[j]<<" ";
cout<<endl;
int i = 0;
node* Head = NULL;
while(i != 10)
{
Head = buildTree(Head,arr[i]);
++i;
}
showTree(Head);
cout<<endl;
return 0;
}
node* buildTree(node* head,int value)
{
node* p = new node;
p->key = value;
p->left = p->right = NULL;
if(NULL == head)
return p;
else
{
if(p->key < head->key)
head->left = buildTree(head->left,value);
else
head->right = buildTree(head->right,value);
return head;
}
}
void showTree(node* head)
{
if(head->left != NULL)
showTree(head->left);
cout<<head->key<<" ";
if(head->right != NULL)
showTree(head->right);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: