Bimbingan Olimpiade Komputer : Pembahasan 3. OSK 2007

Soal 39. OSK 2007
4checks.com-Free Shipping and Handling on All Personal Checks with code DWF008

function apaitu(a:integer; b:integer):integer;
var count : integer;
begin

count :=count + 1;
writeln(count);
if (a > b) then
   apaitu := apaitu(b,a)
else if (a = 0) then
     apaitu :=b
else
    apaitu := apaitu(b mod a,a);
end;

begin
writeln(apaitu(1001,1331));
readln;
end.

Pembahasan :
Pemanggilan writeln(apaitu(1001,1331)) akan menghasilkan 11

A = 1001
B = 1331

if (a>b)  apaitu(a,b) if(a = 0)  apaitu := b apaitu(b mod a, a)
(1001>1331)tidak
                      if(1001=0)tidak     apaitu(1331 mod 1001,1001)
                                               apaitu(330,1001)

(1001>330)ya
            apaitu(330,1001)
(330>1001)tidak
                      if(330=0)tidak    apaitu(1001 mod 330,330)
                                             apaitu(11,330)

(330 > 11) ya
            apaitu(11,330)
(11 > 330) tidak
                      if(11=0) tidak    apaitu(330 mod 11,11)
                                            apaitu(0,11)

(11 > 0) ya
                apaitu(0,11)
(0 > 11) ya
                        if(0=0) yA ---> PROSES SELESAI	      
NILAI A = 11

Link Yang berhubungan :

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: