Advertisement
Guest User

StairCase

a guest
Oct 18th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.65 KB | None | 0 0
  1. {There's a staircase with N steps, and you can climb 1 or 2 steps at a time. Given N, write a function that returns the number of unique ways you can climb the staircase.
  2.  The order of the steps matters.
  3.  
  4. For example, if N is 4, then there are 5 unique ways:
  5.  
  6. 1, 1, 1, 1
  7. 2, 1, 1
  8. 1, 2, 1
  9. 1, 1, 2
  10. 2, 2
  11. What if, instead of being able to climb 1 or 2 steps at a time, you could climb any number from a set of positive integers X? For example,
  12. if X = [1, 3, 5], you could climb 1, 3, or 5 steps at a time. Generalize your function to take in X.   }
  13. program drouj;
  14. uses WinCrt;
  15. type
  16.     tab = Array [1..100] of string;
  17. var
  18.     n : Byte;
  19.     t : tab;        
  20.     Procedure saisie(var n : byte );
  21.         begin
  22.             repeat
  23.                 write('N= ');
  24.                  read(n);
  25.             until (n <=100)and (n>0);
  26.         end;
  27.         function verif(t: tab;k:byte;ch: string): Boolean;
  28.             Var
  29.                 a: byte;
  30.             Begin
  31.                 a:=1;
  32.                 while (t[a]<> ch ) and (a<=k) do
  33.                     a:=a+1;
  34.                 verif := a >k;
  35.             end;
  36.                 Function calcul(n: Byte):integer;
  37.                     Var
  38.                         s,x:integer;
  39.                         ls:longint;
  40.                         a,k:byte;
  41.                         t: tab;
  42.                         tt: toch;
  43.                         ch: string;
  44.                     Begin
  45.                         k:=1;
  46.                         for a:=1 to n*n do
  47.                             begin
  48.                                 s:=0;
  49.                                 ls:=0;
  50.                                 repeat
  51.                                     repeat
  52.                                         x:=random(2)+1;
  53.                                     until x+s <= n ;
  54.                                     s:=s+x;
  55.                                     ls:=ls*10+x;
  56.                                 until s = n ;
  57.  
  58.                                 str(ls,ch);
  59.                                 if a=1 then
  60.                                     t[1] := ch
  61.                                 Else
  62.                                     if verif(t,k,ch) then
  63.                                         begin
  64.                                             k:=k+1;
  65.                                             t[k]:=ch;
  66.                                         end;
  67.                             end;
  68.                             calcul:=k;
  69.                     end;
  70. Begin
  71.     saisie(n);
  72.     write('There are ',calcul(n),' unique ways to climb the stairs');
  73. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement