数据结构第3章例题与答案5
来源:优易学  2010-1-14 18:15:03   【优易学:中国教育考试门户网】   资料下载   IT书店
24.完善下面算法。【中山大学 1998 四、2(6分)】
后缀表达式求值,表达式13/25+61的后缀表达式格式为: 13, 25/61, +
func  compute(a):real;   后缀表达式存储在数组a[1..m]中。
begin
   setnull(s);i:=1;ch:= (1)______;
   while ch<>’@’ do
     begin
       case ch of 
        ‘0’..‘9’:  x:=0;
              while ch<>’,’do
                begin 
                  x:=x*10+ord(ch)-ord(‘0’);
                  i:=i+1;ch:= (2)_______;
                end
‘+’: x:=pop(s)+pop(s);
‘-‘: x:=pop(s);x:=pop(s)-x;
‘*’: x:=pop(s)*pop(s);
‘/’: x:=pop(s);x:=pop(s)/x;
endcase
push(s,x);i:=i+1;ch:=a[i];
end;
comput:= (3)_______;
end; 
25. 算术表达式求值的流程,其中optr为算术符栈,opnd为操作数栈,precede(oper1,oper2)是比较运算符优先级别的函数,operate(opnd1,oper,opnd2)为两操作数的运算结果函数。(#表示运算起始和终止符号)【西北工业大学 1999 六、2 (7分)】
  function  exp_reduced:operandtype;
  initstack(optr);push(optr"#");initstack(opnd);read(w);
  while  not((w=’#’) and (gettop(optr)=’#’)) do
     if not w in op then push(opnd,w);
     else case precede(gettop(optr),w)of
              ’<’:[(1)_______; read(w);]
              ’=’:[(2)_______; read(w);];
              ’>’:[theta:=pop(optr);b:=pop(opnd);a:=pop(opnd);(3)_______;]
          endc;
return(gettop(opnd));
endf; 
26.根据需要,用适当的语句填入下面算法的_______中:
问题:设有n件物品,重量分别为w1,w2,w3,…,wn和一个能装载总重量为t的背包。能否从n件物品中选择若干件恰好使它们的重量之和等于t。若能,则背包问题有解,否则无解。解此问题的算法如下:  
    function kanp_stack(var stack,w:array[1..n] of real; var top:integer; t:real):boolean;
     {w[1:n] 存放n件物品的重量,依次从中取出物品放入背包中,检查背包重量,若不超过t,则装入,否则弃之,取下一个物品试之。若有解则返回函数值true,否则返回false}
begin
    top:=0;  i:=1;               { i指示待选物品}
    while (1)_______ and(2)_______do
       [if (3)______ or (4)_______ and (i          then  [top := (5)_______ ;stack[top] :=i;{第i件物品装入背包}
                 t:=t-w[i]];
        if t=0 then return ((6)_______)  {背包问题有解}
               else  [if  (i=n )  and  (top>0)
                      then  [i:=(7)_______;{取出栈顶物品}
                             top:= (8)_______ ;t:= (9)_______ ];  {恢复t值} 
                       i:=i+1        {准备挑选下一件物品}
                     ];
        ];
    return((10)_______) {背包无解}
end;
【北京邮电大学 1996 四(10分)】 
19. 写出和下列递归过程等价的非递归过程。
procedure   test(var  sum:integer);
    var  a:integer,
begin
      read(a);
      if a=0  then  sum:=1
              else  begin test(sum); sum:=sum*a;end;
write(sum);
end;                【清华大学 1996 二】
20. 试将下列递归过程改写为非递归过程。
        void  test(int  &sum)
        { int  x;
          scanf(x);
          if(x=0) sum=0 else {test(sum); sum+=x;}
          printf(sum);
        }         【北京轻工业学院 2000 三 (15分)】
21. 已知ackermann函数定义如下:
    (1) 写出ack(2,1)的计算过程。
(2) 写出计算ack(m,n)的非递归算法。 【北京航空航天大学 1999  六 (15分)】
22.设计算法以求解从集合{1..n}中选取k(k<=n)个元素的所有组合。例如,从集合{1..4}中选取2个元素的所有组合的输出结果为:1  2,1  3,1  4,2  3, 2  4,3  4。
【合肥工业大学 2000 五、5(8分)】

责任编辑:小草

文章搜索:
 相关文章
热点资讯
热门课程培训