Page 117 - C:\Users\Admin\Desktop\Sach mem upweb\
P. 117

100 Problems & Solutions                                               Trang 117


                      Program Bai62;
                      Uses crt;
                      Const fo = 'chenxau.out';
                            dau: array[1..3] of String[1]= ('', '-', '+');
                            s:array[1..9] of char=('1','2','3','4','5','6','7','8','9');
                      Var d:array[1..9] of String[1];
                          m:longInt;
                          f:text;
                          k:integer;
                          found:boolean;
                      Procedure Init;
                      Begin
                        Write('Cho M=');
                        Readln(m);
                        found:=false;
                      end;
                      Function tinh(s:string):longint;
                      Var i,t:longint;
                          code:integer;
                      Begin
                        i:=length(s);
                        While not(s[i] in ['-','+']) and (i>0) do dec(i);
                        val(copy(s,i+1,length(s)-i),t,code);
                        If i=0 then begin tinh:=t; exit; end
                        else
                           begin
                               delete(s,i,length(s)-i+1);
                               If s[i]='+' then tinh:=t+tinh(s);
                               If s[i]='-' then tinh:=tinh(s)-t;
                           end;
                      End;
                      Procedure Test(i:integer);
                      Var st:string; j:integer;
                      Begin
                        st:='';
                        For j:=1 to i do st:=st+d[j]+s[j];
                        If Tinh(st) = m then begin writeln(f,st); found:=true; end;
                      End;
                      Procedure Try(i:integer);
                      Var j:integer;
                      Begin
                        for j:=1 to 3 do
                        begin
                            d[i]:=dau[j]; Test(i);
                            If i<9 then try(i+1);
                        end;
                      End;
                      BEGIN
                        Clrscr;
                        Init;



                      Tin học & Nhà trường                                       100 Đề Toán - Tin học
   112   113   114   115   116   117   118   119   120   121   122