码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构问题

时间:2015-02-05 21:40:23      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:

 

 



二叉排序


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

输入:

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

输出:

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

样例输入:
5
1 6 5 9 8
样例输出:
1 6 5 9 8
1 5 6 8 9 
5 8 9 6 1
提示:

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

来源:
2005年华中科技大学计算机保研机试真题

 1 #include<iostream>
 2 #include<string>
 3 #include <algorithm>
 4 using namespace std;
 5 struct Node
 6 {
 7  Node *lchild;
 8  Node *rchild;
 9  int v;
10 }T[101]; 
11 int loc;
12 Node *create(int x)
13 {
14     T[loc].lchild=NULL;
15 T[loc].rchild=NULL;
16 T[loc].v=x;
17 return &T[loc++];
18 }
19 Node *insert(Node *t,int x)
20 {
21   if(t==NULL) 
22   {
23      t=create(x);
24  return t;
25   }
26   else if(x<t->v)
27   t->lchild=insert(t->lchild,x);
28   else if(x>t->v)
29   t->rchild=insert(t->rchild,x);
30   return t;
31 }
32 void xian(Node *t)
33 {
34    cout<<t->v<<" ";
35    if(t->lchild!=NULL)  xian(t->lchild);
36    if(t->rchild!=NULL)  xian(t->rchild);
37 }
38 void zhong(Node *t)
39 {
40    if(t->lchild!=NULL)  zhong(t->lchild);
41    cout<<t->v<<" ";
42    if(t->rchild!=NULL)  zhong(t->rchild);
43 }
44 void hou(Node *t)
45 {
46    if(t->lchild!=NULL) hou(t->lchild);
47    if(t->rchild!=NULL)  hou(t->rchild);
48    cout<<t->v<<" ";
49 }
50 int main(){
51     int n,i;
52         while( cin>>n ){
53 loc=0;
54 Node *t=NULL;
55             for(i=0;i<n;i++)
56 {
57    int x;
58    cin>>x;
59                t=insert(t,x);
60 }
61 xian(t);cout<<endl;
62 zhong(t);cout<<endl;
63 hou(t);cout<<endl;
64         }
65 return 0;
66 }

 


二叉树遍历

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:2354

解决:140


题目描述:

二叉树的前序、中序、后序遍历的定义:
前序遍历:对任一子树,先访问跟,然后遍历其左子树,最后遍历其右子树;
中序遍历:对任一子树,先遍历其左子树,然后访问根,最后遍历其右子树;
后序遍历:对任一子树,先遍历其左子树,然后遍历其右子树,最后访问根。
给定一棵二叉树的前序遍历和中序遍历,求其后序遍历(提示:给定前序遍历与中序遍历能够唯一确定后序遍历)。

输入:

两个字符串,其长度n均小于等于26。
第一行为前序遍历,第二行为中序遍历。
二叉树中的结点名称以大写字母表示:A,B,C....最多26个结点。

输出:

输入样例可能有多组,对于每组测试样例,
输出一行,为后序遍历的字符串。

样例输入:
ABC
BAC
FDXEAG
XDEFAG
样例输出:
BCA
XEDGAF
来源:
2006年清华大学计算机研究生机试真题

某王道论坛上的牛人递归代码,牛叉。。。
想试着用这个方法来做 中序、后序  写 前序,但是想不出来,主要后序 从后往前是先右后左的,伤脑筋==


 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4  
 5 string s1,s2;
 6 int a[101],k;
 7  
 8 void postOrder(int l,int h){
 9  
10          if( h >= l ){
11               int n=a[k++];
12               postOrder(l,n-1);
13               postOrder(n+1,h);
14               cout<<s2[n];
15          }
16  }
17   
18 int main(){
19  
20         while( cin>>s1>>s2 ){
21              k = 0;
22              for(int i=0;i<s1.length();++i)
23                    for(int j=0;j<s2.length();++j)
24                           if(s1[i]==s2[j]) a[i]=j;     
25               
26              postOrder(0,s2.length()-1);
27               
28              cout<<endl;
29         }
30 }

 

 





哈夫曼树

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:4550

解决:1929

题目描述:

哈夫曼树,第一行输入一个数n,表示叶结点的个数。需要用这些叶结点生成哈夫曼树,根据哈夫曼树的概念,这些结点有权值,即weight,题目需要输出所有结点的值与权值的乘积之和。

输入:

输入有多组数据。
每组第一行输入一个数n,接着输入n个叶节点(叶节点权值不超过100,2<=n<=1000)。

输出:

输出权值。

样例输入:
5 1 2 2 5 9
样例输出:
37
来源:
2010年北京邮电大学计算机研究生机试真题

 1 #include<iostream>
 2 #include<queue>
 3 using namespace std;
 4  
 5  
 6 int main()
 7 {
 8     int n;
 9     while(cin>>n)
10     {
11       priority_queue< int,vector<int>,greater<int> >  q;
12       int i;
13       for(i=0;i<n;i++)
14       {
15           int n1;
16          cin>>n1;
17           q.push(n1);
18       }
19       int sum=0;
20       while(q.size()>1)
21       {
22           int a=q.top();
23           q.pop();
24           int b=q.top();
25           q.pop();
26          sum=sum+a+b;
27          q.push(a+b);
28       }
29  
30       cout<<sum<<endl;
31     }
32      return 0;
33 }
34 /**************************************************************
35     Problem: 1172
36     User: 2009declan
37     Language: C++
38     Result: Accepted
39     Time:10 ms
40     Memory:1520 kb
41 ****************************************************************/

 

 

简单计算器

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:4521

解决:1688

题目描述:
    读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
输入:
    测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。
输出:
    对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。
样例输入:
1 + 24 + 2 * 5 - 7 / 110
样例输出:
3.0013.36
来源:
2006年浙江大学计算机及软件工程研究生机试真题


这种题目真心不要想的太复杂,要选择好适当的读取输入的格式
王道书上代码就很复杂


 1 #include<iostream>
 2 #include<stack>
 3 #include<iomanip>
 4 using namespace std;
 5 int main()
 6 {
 7         char fuhao;int n1,n;
 8          double temp; 
 9         while(cin>>n1)
10         {
11                 if(n1==0)  break;
12        stack<double> num;
13            num.push(n1);
14        while(cin>>fuhao>>n)
15            {
16                    if(fuhao==+) num.push(n);
17                    else if(fuhao==-) num.push(-n);
18                    else if(fuhao==*) 
19                    {
20                            temp=num.top();
21                            num.pop();
22                            num.push(1.0*temp*n);
23                    }
24                    else
25                    {
26                        temp=num.top();
27                            num.pop();
28                            num.push(1.0*temp/n);
29                    }
30  
31                    if(cin.get()==\n) break;
32            }
33            double sum=0;
34            while(!num.empty())
35            {
36                    sum+=num.top();
37                    num.pop();
38            }
39  
40            cout<<fixed<<setprecision(2)<<sum<<endl;
41         
42         }
43          return 0;
44 }

 

 

















 

 
括号匹配问题

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:2938

解决:1303

 





题目描述:

    在某个字符串(长度不超过100)中有左括号、右括号和大小写字母;规定(与常见的算数式子一样)任何一个左括号都从内到外与在它右边且距离最近的右括号匹配。写一个程序,找到无法匹配的左括号和右括号,输出原来字符串,并在下一行标出不能匹配的括号。不能匹配的左括号用"$"标注,不能匹配的右括号用"?"标注.

输入:

    输入包括多组数据,每组数据一行,包含一个字符串,只包含左右括号和大小写字母,字符串长度不超过100。
    注意:cin.getline(str,100)最多只能输入99个字符!

输出:

    对每组输出数据,输出两行,第一行包含原始输入字符,第二行由"$","?"和空格组成,"$"和"?"表示与之对应的左括号和右括号不能匹配。


 1 #include<iostream>
 2 
 3 #include<stack>
 4 
 5 #include<string>
 6 
 7 using namespace std;
 8 
 9  
10 
11  
12 
13 int main()
14 
15 {
16 
17     string s1,s2;
18 
19     stack<int> s;
20 
21  
22 
23     while(cin>>s1)
24 
25     {
26 
27         s2="";
28 
29         int i;
30 
31         for(i=0;i<s1.length();i++)
32 
33             s2=s2+" ";
34 
35         for(i=0;i<s1.length();i++)
36 
37         {
38 
39            if(s1[i]==() 
40 
41            {
42 
43                s.push(i);
44 
45                s2[i]= ;
46 
47            }
48 
49            else if(s1[i]==))
50 
51            {
52 
53                if(!s.empty())
54 
55                {
56 
57                 s.pop();
58 
59                s2[i]= ;
60 
61                }
62 
63                else
64 
65                s2[i]=?;
66 
67            }
68 
69            else s2[i]= ;  
70 
71         }
72 
73  
74 
75  
76 
77       while(!s.empty())
78 
79       {
80 
81          s2[s.top()]=$;
82 
83          s.pop();
84 
85       }
86 
87  
88 
89  
90 
91       cout<<s1<<endl;
92 
93       cout<<s2<<endl;
94 
95     }
96 
97      return 0;
98 
99 }

 

 

 

来源:
2010年北京大学计算机研究生机试真题




二叉树遍历分数: 1.5

时间限制:1 秒
内存限制:32 兆
特殊判题: 否
提交:65
解决: 29

标签

  • 二叉树
  • 构建
  • 遍历

题目描述

 

编一个程序,读入用户输入的一串先序遍历字符串,根据此字符串建立一个二叉树(以指针方式存储)。
例如如下的先序遍历字符串:
ABC##DE#G##F###
其中“#”表示的是空格,空格字符代表空树。建立起此二叉树以后,再对二叉树进行中序遍历,输出遍历结果。

 

输入格式

 

 

输入包括1行字符串,长度不超过100。

 

 

输出

 

 

可能有多组测试数据,对于每组数据,
输出将输入字符串建立二叉树后中序遍历的序列,每个字符后面都有一个空格。
每个输出结果占一行。

 

 

样例输入

a#b#cdef#####
a##

样例输出

a b f e d c 

提示[+]

*** 提示已隐藏,点击上方 [+] 可显示 ***

分类

  • 华中科技大学究生复试上机真题
  •  
  •  
  •  1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 struct TreeNode
     5 {
     6    char a;
     7    TreeNode *lchild,*rchild;
     8 };
     9 bool build(TreeNode *&root,string s,int &n)
    10 {
    11    char c=s[n];
    12    if(n>s.length()-1) return false;
    13    n++;
    14    if(c==#)  root=NULL;
    15    else
    16    {
    17       root=(TreeNode*)malloc(sizeof(TreeNode));
    18   root->a=c;
    19   build(root->lchild,s,n);
    20   build(root->rchild,s,n);
    21    }
    22    return true;
    23 }
    24 void bianli(TreeNode *root)
    25 {
    26    if(root)
    27    {
    28       if(root->lchild) bianli(root->lchild);
    29   cout<<root->a<<" ";
    30   if(root->rchild) bianli(root->rchild);
    31    }
    32 }
    33 int main()
    34 {
    35  string s;
    36  while(cin>>s)
    37  {
    38  TreeNode *root;
    39  int n=0;
    40      if(build(root,s,n))
    41  {
    42     bianli(root);
    43 cout<<endl;
    44  }
    45  }
    46     return 0;
    47 }

     

  •  


















 

堆栈的使用分数: 1.5

时间限制:1 秒
内存限制:32 兆
特殊判题: 否
提交:72
解决: 30

标签

题目描述

 

堆栈是一种基本的数据结构。堆栈具有两种基本操作方式,push 和 pop。Push一个值会将其压入栈顶,而 pop 则会将栈顶的值弹出。现在我们就来验证一下堆栈的使用。

 

输入格式

 

对于每组测试数据,第一行是一个正整数 n,0<n<=10000(n=0 结束)。而后的 n 行,每行的第一个字符可能是‘P’或者‘O’或者‘A’;如果是‘P’,后面还会跟着一个整数,表示把这个数据压入堆栈;如果是‘O’,表示将栈顶的值 pop 出来,如果堆栈中没有元素时,忽略本次操作;如果是‘A’,表示询问当前栈顶的值,如果当时栈为空,则输出‘E‘。堆栈开始为空。

 

输出

 

 对于每组测试数据,根据其中的命令字符来处理堆栈;并对所有的‘A’操作,输出当时栈顶的值,每个占据一行,如果当时栈为空,则输出‘E’。当每组测试数据完成后,输出一个空行。

 

样例输入

5
P 75
O
O
P 60
A
7
A
O
P 73
P 49
A
O
P 3
0

样例输出

60

E
49

提示[+]

*** 提示已隐藏,点击上方 [+] 可显示 ***

分类

  • 吉林大学究生复试上机真题
  •  
  •  
  •  1 #include <iostream>
     2 #include <stack>
     3 using namespace std;
     4 int main()
     5 {
     6  int n;
     7  while(cin>>n)
     8  {
     9 stack<int> s;
    10     if(n==0) break;
    11 for(int i=0;i<n;i++)
    12 {
    13    char a;
    14    cin>>a;
    15    if(a==A)
    16    {
    17      if(s.empty()) cout<<"E"<<endl;
    18  else  cout<<s.top()<<endl;
    19    }
    20    else if(a==P)
    21    {
    22      int temp;
    23  cin>>temp;
    24  s.push(temp);
    25    }
    26    else if(a==O)
    27    {
    28      if(!s.empty()) s.pop();
    29    }
    30 }
    31 cout<<endl;
    32  }
    33     return 0;
    34 }

     

  •  





 

数据结构问题

标签:

原文地址:http://www.cnblogs.com/xiaoyesoso/p/4275897.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!