笔试面试题

来源:互联网 发布:淘宝有监控软件 编辑:程序博客网 时间:2024/04/28 16:02

1 写一个函数找出一个整数数组中,第二大的数 microsoft

答案:
const int MINNUMBER = -32767 ;
int find_sec_max( int data[] , int count)
{
int maxnumber = data[0] ;
int sec_max = MINNUMBER ;
for ( int i = 1 ; i < count ; i++)
{
if ( data[i] > maxnumber )
{
sec_max = maxnumber ;
maxnumber = data[i] ;
}
else
{
if ( data[i] > sec_max )
sec_max = data[i] ;
}
}
return sec_max ;
}

举一反三,求第 i  大的数,      c程序如下:

#include <iostream.h>
/* ---------------------------------------- */
/*  求最大值                    */
/* ---------------------------------------- */
int max(int *a,int n)
{
 int max,i,m;
 max=a[0];
 for(i=0;i<n;++i){
  if(a[i]>=max){
   max=a[i];
   m=i;
  }
 }
 return m;
}
void findnum(int *a,int n,int num)
{
 
 int i,p,val;
 int *b=a;
 int no=num-1;
 int pos=0;
 while(no){
  p=max(a,n);
  b[p]=0;
  --no;
 }
 val=a[0];
 for(i=0;i<n;++i){
  if((a[i]!=0)&&(a[i]>val)){
   val=a[i];
   pos=i;
  }
 }
 cout<<"第  "<<num<<"   大元素的值:  "<<"a["<<pos<<"]= "<<a[pos]<<endl;
}
/* ---------------------------------------- */
/*  主程序:         
/* ---------------------------------------- */
void main()
{
 int a[10]={46,14,38,74,96,65,8,49,55,27};            
 int i;
 cout<<"/n原始数据为:        ";
 for(i=0;i<10;i++)
  cout<<a[i]<<" ";
 cout<<endl<<endl;
 // findnum(a,10,1);           //  求第1大数的程序(取值1-10)
  // findnum(a,10,2);           //  求第2大数的程序(取值1-10)
 findnum(a,10,3);           //  求第3大数的程序(取值1-10)
}

例7.13---教材p151二叉排序树综合程序

*******************************************************************************************************************************************/

1 :  #include<string.h>
2 :  #include<malloc.h>
3 :  #include<stdio.h>
4 :  #include<iostream.h>
5 :  #define TRUE 1
6 :  #define FALSE 0
7 :  #define KeyType int
8 :  typedef struct node
9 :  {
10 :   KeyType key;
11 :      struct node *lchild,*rchild;
12 :  }BSTNode;
13 : 
14 :  BSTNode *searchnode(int w, BSTNode *r)
15 :  {
16 :   BSTNode *p;
17 :  
18 :   if(r==NULL)
19 :    p=NULL;
20 :   else
21 :   {
22 :    if(w==r->key)
23 :     p=r;
24 :    else
25 :     if(w>r->key)
26 :      p=searchnode(w,r->rchild);
27 :     else
28 :      p=searchnode(w,r->lchild);
29 :   }
30 :   return p;
31 :  }
32 : 
33 :  BSTNode *insert_bst(int w,BSTNode *p)
34 : 
35 :  {
36 :      if(p==NULL)
37 :   {
38 :    p=(BSTNode *)malloc(sizeof(BSTNode));
39 :          p->lchild=NULL;
40 :          p->rchild=NULL;
41 :          p->key=w;
42 :   }
43 :      else
44 :    if(w>p->key)
45 :     p->rchild=insert_bst(w,p->rchild);
46 :    else
47 :     p->lchild=insert_bst(w,p->lchild);
48 :    return p;
49 :  }
50 : 
51 :  BSTNode *getfather(BSTNode *p,BSTNode *r)
52 :  {
53 :   BSTNode *pf;
54 :      if(r==NULL||p==r)
55 :    pf=NULL;
56 :      else
57 :   {
58 :    if(p==r->lchild||p==r->rchild)
59 :     pf=r;
60 :          else
61 :     if(p->key>r->key)
62 :      pf=getfather(p,r->rchild);
63 :              else
64 :      pf=getfather(p,r->lchild);
65 :   }
66 :   return pf;
67 :  }
68 : 
69 :  BSTNode *dele_sbt(BSTNode *p,BSTNode *r)
70 :  {
71 :   BSTNode *temp, *tfather,*pf;
72 :      pf=getfather(p,r);
73 :      if(p->lchild==NULL&&p->rchild==NULL&&pf!=NULL)
74 :   {
75 :    if(pf->lchild==p)
76 :     pf->lchild=NULL;
77 :    else
78 :     pf->rchild=NULL;
79 :   }
80 :    if(p->lchild=NULL&&p->rchild==NULL&&pf==NULL)
81 :     //被删结点是叶子结点,又是根结点.
82 :     r=NULL;
83 :    if(pf->lchild==NULL&&p->rchild!=NULL&&pf!=NULL)
84 :     //被删结点有右孩子,无左孩子,被删结点是根结点.
85 :     if(pf->lchild==p)
86 :      pf->lchild=p->rchild;
87 :     else
88 :      pf->rchild=p->rchild;
89 :     if(p->lchild==NULL&&p->rchild!=NULL&&pf==NULL)
90 :      r=p->rchild;
91 :     if(p->lchild!=NULL&&p->rchild==NULL&&pf!=NULL)
92 :      //被子删结点有左孩子,无左孩子,被删结点是根结点.
93 :      if(pf->lchild==p)
94 :       pf->lchild=p->lchild;
95 :      else
96 :       pf->rchild=p->lchild;
97 :      if(p->lchild!=NULL&&p->rchild==NULL&&pf==NULL)
98 :       //被删结点有左孩子,无右孩子.被删结点是根结点.
99 :       r=p->lchild;
100 :      if(p->lchild!=NULL&&p->rchild!=NULL)
101 :      {
102 :       temp=p->lchild;tfather=p;
103 :       while(temp->rchild!=NULL)
104 :       {
105 :        tfather=temp;
106 :        temp=temp->rchild;
107 :       }
108 :       p->key=temp->key;
109 :       if(tfather!=p)
110 :        tfather->lchild=temp->lchild;
111 :       else
112 :        tfather->lchild=temp->lchild;
113 :      }
114 :      cout<<endl;
115 :      if(r!=NULL)
116 :       cout<<"二叉排序树的根是,%d"<<r->key<<endl;
117 :      else
118 :       cout<<"二叉排序树为空!"<<endl;
119 :      return r;
120 :  }
121 :  int at(char *str,char ch)
122 :  {
123 :   int r;
124 :   while(*str!='/0'&& *str!=ch)
125 :    str++;
126 :   r=(*str==ch);
127 :   return r;
128 :  }
129 :  void print_bst(BSTNode *p)
130 :  //中序遍历二叉排树,并显示遍历结果.
131 :  {
132 :   if(p!=NULL)
133 :   {
134 :    print_bst(p->lchild);
135 :          cout<<p->key<<endl;
136 :          if(p->lchild!=NULL)
137 :     cout<<p->key<<"的左结点:"<<p->lchild->key<<endl;
138 :          else
139 :              cout<<p->key<<"的左结点=NULL"<<endl;
140 :    if(p->rchild!=NULL)
141 :     cout<<p->key<<"的右结点:"<<p->rchild->key<<endl;
142 :    else
143 :     cout<<p->key<<"的右结点"<<endl;
144 :    cout<<endl;
145 :    print_bst(p->rchild);
146 :   }
147 :  }
148 : 
149 :  BSTNode *create_bst()
150 :  //建立二叉排序树模块
151 :  {
152 :   BSTNode *root,*p;
153 :   int loop=FALSE;
154 :      int s;
155 :      root=NULL;
156 :      cout<<"如果要退出,请按<0>"<<endl;
157 :      do
158 :   {
159 :    cout<<endl;
160 :    cout<<"请输入一个整数:"<<endl;
161 :    cin>>s;
162 :    if(s==0)
163 :     loop=TRUE;
164 :    else
165 :    {
166 :     p=searchnode(s,root);
167 :     if(p==NULL)
168 :     {
169 :      root=insert_bst(s,root);
170 :      //将元素插入二叉排序树.
171 :      //print_bst(root);
172 :     }
173 :    }
174 :    if(root!=NULL)
175 :     cout<<"二叉排序树的根为:"<<(root->key)<<endl;
176 :   }
177 :   while(!loop);
178 :   return root;
179 :  }
180 : 
181 :  BSTNode *insert(BSTNode *root)
182 :  //结点插入二叉树模块.
183 :  {
184 :   BSTNode *p;
185 :      int s;
186 :      cout<<endl;
187 :      cout<<"输入一个整数:"<<endl;
188 :      cin>>s;
189 :      if(s!=0)
190 :   {
191 :    p=searchnode(s,root);
192 :          if(p==NULL)
193 :    {
194 :     root=insert_bst(s,root);
195 :              print_bst(root);
196 :              if(root!=NULL)
197 :      cout<<"二叉排序树的为:"<<root->key<<endl;
198 :    }
199 :    else
200 :     cout<<"该结点值存在,不插入!"<<endl;
201 :   }
202 :   return root;
203 :  }
204 : 
205 :  void search_bst(BSTNode *root)
206 :  //查询模块.
207 :  {
208 :   int s;
209 :      BSTNode *p;
210 :      cout<<endl;
211 :      cout<<"输入要查询的结点值";
212 :      cin>>s;
213 :      if(s!=0)
214 :   {
215 :    p=searchnode(s,root);
216 :     if(p==NULL)
217 :      cout<<"该结点不存在!"<<endl;
218 :     else
219 :      cout<<"该结点存在!"<<endl;
220 :   }
221 :  }
222 : 
223 :  BSTNode *ddelete(BSTNode *root)
224 : 
225 :  {
226 :   int s;
227 :      BSTNode *p;
228 :      char ch[5];
229 :      cout<<endl;
230 :      cout<<"输入要删除的结点值:";
231 :    cin>>s;
232 :      if(s!=0)
233 :   {
234 :    p=searchnode(s,root);
235 :          if(p==NULL)
236 :     cout<<"该结点值不存在!"<<endl;
237 :          else
238 :    {
239 :     cout<<"该结点值存在,要删除吗?(Y/N)"<<endl;
240 :              getchar();
241 :              gets(ch);
242 :              if(at(ch,'y')||at(ch,'Y'))
243 :      root=dele_sbt(p,root);
244 :    }
245 :   }
246 :   print_bst(root);
247 :   return(root);
248 :  }
249 : 
250 :  
251 : 
252 :  void main()
253 :  {
254 : 
255 :   BSTNode *root;
256 :   int loop,i;
257 :   loop=TRUE;
258 :   while(loop)
259 :   {
260 :    cout<<"例7.13---教材p151二叉排序树综合程序"<<"/n说明  序号:二叉排序树----对应序号的功能说明"<<endl;
261 :    cout<<"["<<endl;
262 :    cout<<"      1:   二叉排序树----建立"<<endl;//退出
263 :    cout<<"      2:   二叉排序树----插入"<<endl;//建立
264 :    cout<<"      3:   二叉排序树----查询"<<endl;//插入
265 :    cout<<"      4:   二叉排序树----删除"<<endl;//查询
266 :    cout<<"      5:   二叉排序树----显示"<<endl;//删除
267 :    cout<<"      0:   exit main"<<endl<<"]"<<endl;//显示
268 :    cout<<"请输入序号以执行相应操作:      ";
269 :    cin>>i;
270 :    switch(i)
271 :    {
272 :    case 0:loop=FALSE;break;        
273 :    case 1:root=create_bst();break;
274 :    case 2:root=insert(root);break;
275 :    case 3:search_bst(root);break; 
276 :    case 4:root=ddelete(root);break; 
277 :    case 5:cout<<endl;
278 :     if(root!=NULL)
279 :      cout<<" 二叉排序树的根为:"<<root->key<<endl;
280 :     print_bst(root);
281 :     break;
282 :    }
283 :   }
284 :  }

 

/*------------N 皇后问题-----------------------------*/

1 :  /* 应用递归来解    数字1: 表示是放置皇后  数字 0: 表示没有放置                     */
2 : 
3 :  #include <stdio.h>
4 :  #include <stdlib.h>
5 :  #define MAXQUEEN   8              /* 最大放置的皇后数   */
6 : 
7 :  int pad[MAXQUEEN][MAXQUEEN] = {   /* N X N 的棋盘       */
8 :           0, 0, 0, 0, 0, 0, 0, 0,
9 :           0, 0, 0, 0, 0, 0, 0, 0,
10 :           0, 0, 0, 0, 0, 0, 0, 0,
11 :           0, 0, 0, 0, 0, 0, 0, 0,
12 :           0, 0, 0, 0, 0, 0, 0, 0,
13 :           0, 0, 0, 0, 0, 0, 0, 0,
14 :           0, 0, 0, 0, 0, 0, 0, 0,
15 :           0, 0, 0, 0, 0, 0, 0, 0 
16 :  };
17 : 
18 :  int place(int ,int );
19 :  /* ---------------------------------------- */
20 :  /*  放 N 个皇后的递归函数                   */
21 :  /* ---------------------------------------- */
22 :  int put_queen(int x,int y,int times)
23 :  {
24 :     int i,j,result = 0;
25 : 
26 :     if ( times > MAXQUEEN )        /* 终止条件           */
27 :        return 1;
28 :     else
29 :        if ( place(x,y) )           /* 检查是否可放置皇后 */
30 :        {
31 :           pad[x][y] = 1;           /* 放置皇后           */
32 :           for ( i = 0; i < MAXQUEEN; i++)
33 :              for ( j = 0; j < MAXQUEEN; j++)
34 :              {
35 :                 /* 递归调用放置下一个皇后 */
36 :                 result += put_queen(i,j,times + 1);
37 :                 if ( result > 0 )
38 :                    break;
39 :              }
40 :           if ( result > 0 )        /* 找到了解           */
41 :              return 1;
42 :           else
43 :           {
44 :              pad[x][y] = 0;        /* 清除皇后           */
45 :              return 0;
46 :           }
47 :        }
48 :        else
49 :           return 0;
50 :  }
51 : 
52 :  /* ---------------------------------------- */
53 :  /*  检查皇后是否有相互攻击                  */
54 :  /* ---------------------------------------- */
55 :  int place(int x,int y)
56 :  {
57 :     int x1,y1;
58 : 
59 :     if ( pad[x][y] != 0 )          /* 己放置皇后         */
60 :        return 0;
61 :     x1 = x - 1;                    /* 检查左上方         */
62 :     y1 = y - 1;
63 :     while ( x1 >= 0 && y1 >= 0 )
64 :        if ( pad[x1--][y1--] != 0 )
65 :           return 0;
66 :     x1 = x + 1;                    /* 检查右下方         */
67 :     y1 = y + 1;
68 :     while ( x1 < MAXQUEEN && y1 < MAXQUEEN )
69 :        if ( pad[x1++][y1++] != 0 )
70 :           return 0;
71 :     x1 = x + 1;                    /* 检查右上方         */
72 :     y1 = y - 1;
73 :     while ( x1 < MAXQUEEN && y1 >= 0 )
74 :        if ( pad[x1++][y1--] != 0 )
75 :           return 0;
76 :     x1 = x - 1;                    /* 检查左下方         */
77 :     y1 = y + 1;
78 :     while ( x1 >=0 && y1 < MAXQUEEN )
79 :        if ( pad[x1--][y1++] != 0 )
80 :           return 0;
81 :     x1 = x;                        /* 检查下方           */
82 :     y1 = y + 1;
83 :     while ( y1 < MAXQUEEN )
84 :        if ( pad[x1][y1++] != 0 )
85 :           return 0;
86 :     x1 = x;                        /* 检查上方           */
87 :     y1 = y - 1;
88 :     while ( y1 >= 0 )
89 :        if ( pad[x1][y1--] != 0 )
90 :           return 0;
91 :     x1 = x + 1;                    /* 检查右方           */
92 :     y1 = y;
93 :     while ( x1 < MAXQUEEN )
94 :        if ( pad[x1++][y1] != 0 )
95 :           return 0;
96 :     x1 = x - 1;                    /* 检查左方           */
97 :     y1 = y;
98 :     while ( x1 >= 0 )
99 :        if ( pad[x1--][y1] != 0 )
100 :           return 0;
101 :     return 1;
102 :  }
103 : 
104 :  /* ---------------------------------------- */
105 :  /*  主程式: 用递归的方法解 N = 8 皇后问题.  */
106 :  /* ---------------------------------------- */
107 :  void main()
108 :  {
109 :     int i,j;
110 : 
111 :     put_queen(0,0,1);              /* 调用递归函数       */
112 :     printf("放置八皇后的棋盘图形:/n");
113 :     for ( i = 0; i < MAXQUEEN; i++)   /* 印出棋盘的图形  */
114 :     {
115 :        for ( j = 0; j < MAXQUEEN; j++)
116 :           printf("%d  ",pad[i][j]);  /* 印出各座标值       */
117 :        printf("/n");               /* 换行               */
118 :     }
119 :  }

/*------------栈的应用:求表达式值-------------*/

1 :   /*栈的应用:求表达式值*/
2 :      #include <stdio.h>
3 :   #include <stdlib.h>
4 :      #define MAX 100
5 :      char exp[MAX];   /*存储转换成的后缀表达式*/
6 :      void trans()     /*将算术表达式转换成后缀表达式*/
7 :      {
8 :        char str[MAX];   /*存储原算术表达式*/
9 :        char stack[MAX]; /*作为栈使用*/
10 :        char ch;
11 :        int sum,i,j,t,top=0;
12 :               /*t作为exp的下标,top作为stack的下标,i作为str的下标*/
13 :        printf("***************************************************************/n");
14 :        printf("* 输入一个求值的表达式,以#结束。只能包含+,-,*,/运算符和正整数 */n");
15 :        printf("***************************************************************/n");
16 :        printf("算术表达式:");
17 :        i=0;   /*获取用户输入的表达式*/
18 :        do
19 :        {
20 :           i++;
21 :    scanf("%c",&str[i]);
22 :        } while (str[i]!='#' && i!=MAX);
23 :        sum=i; /*记录输入表达式总的字符个数*/
24 :        t=1;i=1;
25 :        ch=str[i];i++;
26 :        while (ch!='#')
27 :        {
28 :   switch(ch)
29 :   {
30 :     case '(':  /*判定为左括号*/
31 :       top++;stack[top]=ch;
32 :       break;
33 :     case ')':  /*判定为右括号*/
34 :       while (stack[top]!='(')
35 :       {
36 :         exp[t]=stack[top];top--;t++;
37 :       }
38 :       top--;
39 :       break;
40 :     case '+':   /*判定为加减号*/
41 :     case '-':
42 :        while (top!=0 && stack[top]!='(')
43 :        {
44 :          exp[t]=stack[top];top--;t++;
45 :        }
46 :        top++;stack[top]=ch;
47 :        break;
48 :      case '*':  /*判定为'*'或'/'号*/
49 :      case '/':
50 :        while (stack[top]=='*' || stack[top]=='/')
51 :        {
52 :                    exp[t]=stack[top];top--;t++;
53 :        }
54 :        top++;stack[top]=ch;
55 :        break;
56 :      case ' ':break;
57 :      default:
58 :        while (ch>='0' && ch<='9') /*判定为数字*/
59 :        {
60 :           exp[t]=ch;t++;
61 :           ch=str[i];i++;
62 :        }
63 :        i--;
64 :        exp[t]='#';t++;
65 :    }
66 :    ch=str[i];i++;
67 :       }
68 :       while (top!=0)
69 :       {
70 :          exp[t]=stack[top];t++;top--;
71 :       }
72 :       exp[t]='#';
73 :       printf("/n/t原来表达式:");
74 :       for (j=1;j<sum;j++) printf("%c",str[j]);
75 :       printf("/n/t后缀表达式:",exp);
76 :       for (j=1;j<t;j++) printf("%c",exp[j]);
77 :     }
78 :      void compvalue()  /*计算后缀表达式的值*/
79 :      {
80 :        float stack[MAX],d; /*作为栈使用*/
81 :        char ch;
82 :        int t=1,top=0;  /*t作为exp的下标,top作为stack的下标*/
83 :        ch=exp[t];t++;
84 :        while (ch!='#')
85 :        {
86 :           switch (ch)
87 :           {
88 :      case '+':stack[top-1]=stack[top-1]+stack[top];
89 :        top--;break;
90 :      case '-':stack[top-1]=stack[top-1]-stack[top];
91 :        top--;break;
92 :      case '*':stack[top-1]=stack[top-1]*stack[top];
93 :        top--;break;
94 :      case '/':if (stack[top]!=0)
95 :           stack[top-1]=stack[top-1]/stack[top];
96 :        else
97 :        {
98 :           printf("/n/t除零错误!/n");
99 :                         exit(0);/*异常退出*/
100 :        }
101 :               top--;break;
102 :      default:
103 :                      d=0;
104 :                      while (ch>='0' && ch<='9')   /*判定为数字字符*/
105 :                      {
106 :          d=10*d+ch-'0';  /*将数字字符转换成对应的数值*/
107 :          ch=exp[t];t++;
108 :        }
109 :        top++;
110 :        stack[top]=d;
111 :     }
112 :            ch=exp[t];t++;
113 :         }
114 :         printf("/n/t计算结果:%g/n",stack[top]);
115 :      }
116 : 
117 :      void main()
118 :      {
119 :        trans();
120 :        compvalue();
121 :      }
122 : 


/*哈希表基本操作*/
1 :  #include <stdio.h>
2 :  #include <stdlib.h>
3 :  #include <string.h>
4 :  #define M  11
5 :  #define N  8
6 :  struct hterm
7 :  {
8 :   int key;   /*关键字值*/
9 :   int si;    /*散列次数*/
10 :  };
11 :  struct hterm hashlist[M+1];
12 :  int i,address,sum,d,x[N+1];
13 :  float average;
14 :  int main()
15 :  {
16 :   for (i=1;i<=M;i++)  /*置初值*/
17 :   {
18 :    hashlist[i].key=0;
19 :    hashlist[i].si=0;
20 :   }
21 :   x[1]=22;  x[2]=41;   x[3]=53;
22 :   x[4]=46;  x[5]=30;   x[6]=13;
23 :   x[7]=1;   x[8]=67;
24 :   for (i=1;i<=N;i++)
25 :   {
26 :    sum=0;
27 :    address=(3*x[i]) % M;
28 :    d=address;
29 :    if (hashlist[address].key==0)
30 :    {
31 :     hashlist[address].key=x[i];
32 :     hashlist[address].si=1;
33 :    }
34 :    else
35 :    {
36 :     do  /*处理冲突*/
37 :     {
38 :      d=(d+(x[i]*7) % 10 +1) % 11;
39 :                  sum=sum+1;
40 :     } while (hashlist[d].key!=0);
41 :     hashlist[d].key=x[i];
42 :     hashlist[d].si=sum+1;
43 :    }
44 :   }
45 :   printf("哈希表地址:  ");
46 :   for (i=0;i<M;i++) printf("%-4d",i);
47 :   printf("/n");
48 :   printf("哈希表关键字:");
49 :   for (i=0;i<M;i++) printf("%-4d",hashlist[i].key);
50 :   printf("/n");
51 :   printf("搜索长度:    ");
52 :   for (i=0;i<M;i++) printf("%-4d",hashlist[i].si);
53 :   printf("/n");
54 :   average=0;
55 :   for (i=0;i<M;i++) average=average+hashlist[i].si;
56 :   average=average/N;
57 :   printf("平均搜索长度:ASL(%d)=%g/n",N,average);
58 :   return 0;
59 :  }

 


/////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

#include<string.h>
#include<malloc.h>
#include<stdio.h>
#include<iostream.h>
#define TRUE 1
#define FALSE 0
#define KeyType int
typedef struct node
{
 KeyType key;
    struct node *lchild,*rchild;
}BSTNode;

BSTNode *searchnode(int w, BSTNode *r)
{
 BSTNode *p;
 
 if(r==NULL)
  p=NULL;
 else
 {
  if(w==r->key)
   p=r;
  else
   if(w>r->key)
    p=searchnode(w,r->rchild);
   else
    p=searchnode(w,r->lchild);
 }
 return p;
}

BSTNode *insert_bst(int w,BSTNode *p)

{
    if(p==NULL)
 {
  p=(BSTNode *)malloc(sizeof(BSTNode));
        p->lchild=NULL;
        p->rchild=NULL;
        p->key=w;
 }
    else
  if(w>p->key)
   p->rchild=insert_bst(w,p->rchild);
  else
   p->lchild=insert_bst(w,p->lchild);
  return p;
}

BSTNode *getfather(BSTNode *p,BSTNode *r)
{
 BSTNode *pf;
    if(r==NULL||p==r)
  pf=NULL;
    else
 {
  if(p==r->lchild||p==r->rchild)
   pf=r;
        else
   if(p->key>r->key)
    pf=getfather(p,r->rchild);
            else
    pf=getfather(p,r->lchild);
 }
 return pf;
}

BSTNode *dele_sbt(BSTNode *p,BSTNode *r)
{
 BSTNode *temp, *tfather,*pf;
    pf=getfather(p,r);
    if(p->lchild==NULL&&p->rchild==NULL&&pf!=NULL)
 {
  if(pf->lchild==p)
   pf->lchild=NULL;
  else
   pf->rchild=NULL;
 }
  if(p->lchild=NULL&&p->rchild==NULL&&pf==NULL)
   //被删结点是叶子结点,又是根结点.
   r=NULL;
  if(pf->lchild==NULL&&p->rchild!=NULL&&pf!=NULL)
   //被删结点有右孩子,无左孩子,被删结点是根结点.
   if(pf->lchild==p)
    pf->lchild=p->rchild;
   else
    pf->rchild=p->rchild;
   if(p->lchild==NULL&&p->rchild!=NULL&&pf==NULL)
    r=p->rchild;
   if(p->lchild!=NULL&&p->rchild==NULL&&pf!=NULL)
    //被子删结点有左孩子,无左孩子,被删结点是根结点.
    if(pf->lchild==p)
     pf->lchild=p->lchild;
    else
     pf->rchild=p->lchild;
    if(p->lchild!=NULL&&p->rchild==NULL&&pf==NULL)
     //被删结点有左孩子,无右孩子.被删结点是根结点.
     r=p->lchild;
    if(p->lchild!=NULL&&p->rchild!=NULL)
    {
     temp=p->lchild;tfather=p;
     while(temp->rchild!=NULL)
     {
      tfather=temp;
      temp=temp->rchild;
     }
     p->key=temp->key;
     if(tfather!=p)
      tfather->lchild=temp->lchild;
     else
      tfather->lchild=temp->lchild;
    }
    cout<<endl;
    if(r!=NULL)
     cout<<"二叉排序树的根是,%d"<<r->key<<endl;
    else
     cout<<"二叉排序树为空!"<<endl;
    return r;
}
int at(char *str,char ch)
{
 int r;
 while(*str!='/0'&& *str!=ch)
  str++;
 r=(*str==ch);
 return r;
}
void print_bst(BSTNode *p)
//中序遍历二叉排树,并显示遍历结果.
{
 if(p!=NULL)
 {
  print_bst(p->lchild);
        cout<<p->key<<endl;
        if(p->lchild!=NULL)
   cout<<p->key<<"的左结点:"<<p->lchild->key<<endl;
        else
            cout<<p->key<<"的左结点=NULL"<<endl;
  if(p->rchild!=NULL)
   cout<<p->key<<"的右结点:"<<p->rchild->key<<endl;
  else
   cout<<p->key<<"的右结点"<<endl;
  cout<<endl;
  print_bst(p->rchild);
 }
}

BSTNode *create_bst()
//建立二叉排序树模块
{
 BSTNode *root,*p;
 int loop=FALSE;
    int s;
    root=NULL;
    cout<<"如果要退出,请按<0>"<<endl;
    do
 {
  cout<<endl;
  cout<<"请输入一个整数:"<<endl;
  cin>>s;
  if(s==0)
   loop=TRUE;
  else
  {
   p=searchnode(s,root);
   if(p==NULL)
   {
    root=insert_bst(s,root);
    //将元素插入二叉排序树.
    //print_bst(root);
   }
  }
  if(root!=NULL)
   cout<<"二叉排序树的根为:"<<(root->key)<<endl;
 }
 while(!loop);
 return root;
}

BSTNode *insert(BSTNode *root)
//结点插入二叉树模块.
{
 BSTNode *p;
    int s;
    cout<<endl;
    cout<<"输入一个整数:"<<endl;
    cin>>s;
    if(s!=0)
 {
  p=searchnode(s,root);
        if(p==NULL)
  {
   root=insert_bst(s,root);
            print_bst(root);
            if(root!=NULL)
    cout<<"二叉排序树的为:"<<root->key<<endl;
  }
  else
   cout<<"该结点值存在,不插入!"<<endl;
 }
 return root;
}

void search_bst(BSTNode *root)
//查询模块.
{
 int s;
    BSTNode *p;
    cout<<endl;
    cout<<"输入要查询的结点值";
    cin>>s;
    if(s!=0)
 {
  p=searchnode(s,root);
   if(p==NULL)
    cout<<"该结点不存在!"<<endl;
   else
    cout<<"该结点存在!"<<endl;
 }
}

BSTNode *ddelete(BSTNode *root)

{
 int s;
    BSTNode *p;
    char ch[5];
    cout<<endl;
    cout<<"输入要删除的结点值:";
  cin>>s;
    if(s!=0)
 {
  p=searchnode(s,root);
        if(p==NULL)
   cout<<"该结点值不存在!"<<endl;
        else
  {
   cout<<"该结点值存在,要删除吗?(Y/N)"<<endl;
            getchar();
            gets(ch);
            if(at(ch,'y')||at(ch,'Y'))
    root=dele_sbt(p,root);
  }
 }
 print_bst(root);
 return(root);
}

 

void main()
{

 BSTNode *root;
 int loop,i;
 loop=TRUE;
 while(loop)
 {
  cout<<"例7.13---教材p151二叉排序树综合程序"<<"/n说明  序号:二叉排序树----对应序号的功能说明"<<endl;
  cout<<"["<<endl;
  cout<<"      1:   二叉排序树----建立"<<endl;//退出
  cout<<"      2:   二叉排序树----插入"<<endl;//建立
  cout<<"      3:   二叉排序树----查询"<<endl;//插入
  cout<<"      4:   二叉排序树----删除"<<endl;//查询
  cout<<"      5:   二叉排序树----显示"<<endl;//删除
  cout<<"      0:   exit main"<<endl<<"]"<<endl;//显示
  cout<<"请输入序号以执行相应操作:      ";
  cin>>i;
  switch(i)
  {
  case 0:loop=FALSE;break;        
  case 1:root=create_bst();break;
  case 2:root=insert(root);break;
  case 3:search_bst(root);break; 
  case 4:root=ddelete(root);break; 
  case 5:cout<<endl;
   if(root!=NULL)
    cout<<" 二叉排序树的根为:"<<root->key<<endl;
   print_bst(root);
   break;
  }
 }
}

/*    应用递归来解 N 皇后问题     数字 1: 表示是放置皇后    数字 0: 表示没有放置                     */

#include <stdio.h>
#include <stdlib.h>
#define MAXQUEEN   8              /* 最大放置的皇后数   */

int pad[MAXQUEEN][MAXQUEEN] = {   /* N X N 的棋盘       */
         0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0 
};

int place(int ,int );
/* ---------------------------------------- */
/*  放 N 个皇后的递归函数                   */
/* ---------------------------------------- */
int put_queen(int x,int y,int times)
{
   int i,j,result = 0;

   if ( times > MAXQUEEN )        /* 终止条件           */
      return 1;
   else
      if ( place(x,y) )           /* 检查是否可放置皇后 */
      {
         pad[x][y] = 1;           /* 放置皇后           */
         for ( i = 0; i < MAXQUEEN; i++)
            for ( j = 0; j < MAXQUEEN; j++)
            {
               /* 递归调用放置下一个皇后 */
               result += put_queen(i,j,times + 1);
               if ( result > 0 )
                  break;
            }
         if ( result > 0 )        /* 找到了解           */
            return 1;
         else
         {
            pad[x][y] = 0;        /* 清除皇后           */
            return 0;
         }
      }
      else
         return 0;
}

/* ---------------------------------------- */
/*  检查皇后是否有相互攻击                  */
/* ---------------------------------------- */
int place(int x,int y)
{
   int x1,y1;

   if ( pad[x][y] != 0 )          /* 己放置皇后         */
      return 0;
   x1 = x - 1;                    /* 检查左上方         */
   y1 = y - 1;
   while ( x1 >= 0 && y1 >= 0 )
      if ( pad[x1--][y1--] != 0 )
         return 0;
   x1 = x + 1;                    /* 检查右下方         */
   y1 = y + 1;
   while ( x1 < MAXQUEEN && y1 < MAXQUEEN )
      if ( pad[x1++][y1++] != 0 )
         return 0;
   x1 = x + 1;                    /* 检查右上方         */
   y1 = y - 1;
   while ( x1 < MAXQUEEN && y1 >= 0 )
      if ( pad[x1++][y1--] != 0 )
         return 0;
   x1 = x - 1;                    /* 检查左下方         */
   y1 = y + 1;
   while ( x1 >=0 && y1 < MAXQUEEN )
      if ( pad[x1--][y1++] != 0 )
         return 0;
   x1 = x;                        /* 检查下方           */
   y1 = y + 1;
   while ( y1 < MAXQUEEN )
      if ( pad[x1][y1++] != 0 )
         return 0;
   x1 = x;                        /* 检查上方           */
   y1 = y - 1;
   while ( y1 >= 0 )
      if ( pad[x1][y1--] != 0 )
         return 0;
   x1 = x + 1;                    /* 检查右方           */
   y1 = y;
   while ( x1 < MAXQUEEN )
      if ( pad[x1++][y1] != 0 )
         return 0;
   x1 = x - 1;                    /* 检查左方           */
   y1 = y;
   while ( x1 >= 0 )
      if ( pad[x1--][y1] != 0 )
         return 0;
   return 1;
}

/* ---------------------------------------- */
/*  主程式: 用递归的方法解 N = 8 皇后问题.  */
/* ---------------------------------------- */
void main()
{
   int i,j;

   put_queen(0,0,1);              /* 调用递归函数       */
   printf("放置八皇后的棋盘图形:/n");
   for ( i = 0; i < MAXQUEEN; i++)   /* 印出棋盘的图形  */
   {
      for ( j = 0; j < MAXQUEEN; j++)
         printf("%d  ",pad[i][j]);  /* 印出各座标值       */
      printf("/n");               /* 换行               */
   }
}

 
原创粉丝点击