Advertisement
ridjis

petak

Dec 23rd, 2013
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. MODULE petak;
  2. FROM InOut IMPORT WriteString, WriteLn, WriteInt, ReadInt;
  3. CONST maxR=20;
  4.       maxN=30;
  5. TYPE niz=ARRAY [0..maxN] OF INTEGER;
  6.  
  7. PROCEDURE grec(n,r: INTEGER): INTEGER; FORWARD;
  8. PROCEDURE frec(n,r:INTEGER):INTEGER;
  9. BEGIN
  10.   IF n<r THEN
  11.     RETURN 2
  12.   ELSE
  13.     IF ODD(n) THEN
  14.       RETURN frec(n-2,r)-grec(n-1,r)+frec(n-r+1,r)-grec(n-r,r)
  15.     ELSE
  16.       RETURN -frec(n-1,r)+grec(n-1,r)-grec(n-2,r)-frec(n-r,r)+grec(n-r,r);
  17.     END;
  18.   END;
  19. END frec;
  20. PROCEDURE grec(n,r:INTEGER):INTEGER;
  21. BEGIN
  22.   IF n<r THEN
  23.     RETURN -1
  24.   ELSE
  25.     RETURN 2*grec(n-1,r)-2*frec(n-r,r)+grec(n-r+1,r)
  26.   END;
  27. END grec;
  28.  
  29. PROCEDURE frec2(n,r: INTEGER): INTEGER;
  30.   PROCEDURE F(f,g: niz; i:INTEGER): INTEGER;
  31.   VAR j: INTEGER;
  32.   BEGIN
  33.   IF i>n THEN
  34.     RETURN f[r]
  35.   ELSE
  36.     IF ODD(i) THEN
  37.       f[r]:=f[r-2]-g[r-1]+f[1]-g[0];
  38.     ELSE
  39.       f[r]:=-f[r-1]+g[r-1]-g[r-2]-f[0]+g[0];
  40.     END;
  41.       g[r]:=2*g[r-1]-2*f[0]+g[1];
  42.       FOR j:=0 TO r-1 DO
  43.         f[j]:=f[j+1];
  44.         g[j]:=g[j+1];
  45.       END;
  46.       RETURN F(f,g,i+1);
  47.     END;
  48.   END F;
  49. VAR f,g:niz;
  50.     j: INTEGER;
  51. BEGIN
  52.   IF n<r THEN
  53.     RETURN 2
  54.   ELSE
  55.     FOR j:=0 TO r-1 DO
  56.       f[j]:=2;
  57.       g[j]:=-1;
  58.     END;
  59.     RETURN F(f,g,r);
  60.   END;
  61. END frec2;
  62.  
  63. PROCEDURE fiter(n,r: INTEGER):INTEGER;
  64. VAR f,g:niz;
  65.     i,j: INTEGER;
  66. BEGIN
  67.   FOR i:=0 TO r-1 DO
  68.     f[i]:=2;
  69.     g[i]:=-1;
  70.   END;
  71.   IF n<r THEN
  72.     RETURN f[n]
  73.   ELSE
  74.     FOR i:=r TO n DO
  75.       IF ODD(i) THEN
  76.         f[r]:=f[r-2]-g[r-1]+f[1]-g[0];
  77.       ELSE
  78.         f[r]:=-f[r-1]+g[r-1]-g[r-2]-f[0]+g[0];
  79.       END;
  80.       g[r]:=2*g[r-1]-2*f[0]+g[1];
  81.       FOR j:=0 TO r-1 DO
  82.         f[j]:=f[j+1];
  83.         g[j]:=g[j+1];
  84.       END;
  85.      END;
  86.      RETURN f[r];
  87.   END;
  88. END fiter;
  89.  
  90. VAR n,r: INTEGER;
  91. BEGIN
  92.   REPEAT
  93.     WriteString('Unesite n (0<=n<= ');
  94.     WriteInt(maxN,1);
  95.     WriteString('): ');
  96.     ReadInt(n);
  97.     WriteLn;
  98.   UNTIL (0<=n) AND (n<=maxN);
  99.   REPEAT
  100.     WriteString('Unesite r (5<=r<= ');
  101.     WriteInt(maxR,1);
  102.     WriteString('): ');
  103.     ReadInt(r);
  104.     WriteLn;
  105.   UNTIL (0<=r) AND (r<=maxR);
  106.   WriteString('frec1(n,r) = ');
  107.   WriteInt(frec(n,r),0);
  108.   WriteLn;
  109.   WriteString('frec2(n,r) = ');
  110.   WriteInt(frec2(n,r),0);
  111.   WriteLn;
  112.   WriteString('fiter(n,r) = ');
  113.   WriteInt(fiter(n,r),0);
  114.   WriteLn;
  115. END petak.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement