Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {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.
- The order of the steps matters.
- For example, if N is 4, then there are 5 unique ways:
- 1, 1, 1, 1
- 2, 1, 1
- 1, 2, 1
- 1, 1, 2
- 2, 2
- 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,
- if X = [1, 3, 5], you could climb 1, 3, or 5 steps at a time. Generalize your function to take in X. }
- program drouj;
- uses WinCrt;
- type
- tab = Array [1..100] of string;
- var
- n : Byte;
- t : tab;
- Procedure saisie(var n : byte );
- begin
- repeat
- write('N= ');
- read(n);
- until (n <=100)and (n>0);
- end;
- function verif(t: tab;k:byte;ch: string): Boolean;
- Var
- a: byte;
- Begin
- a:=1;
- while (t[a]<> ch ) and (a<=k) do
- a:=a+1;
- verif := a >k;
- end;
- Function calcul(n: Byte):integer;
- Var
- s,x:integer;
- ls:longint;
- a,k:byte;
- t: tab;
- tt: toch;
- ch: string;
- Begin
- k:=1;
- for a:=1 to n*n do
- begin
- s:=0;
- ls:=0;
- repeat
- repeat
- x:=random(2)+1;
- until x+s <= n ;
- s:=s+x;
- ls:=ls*10+x;
- until s = n ;
- str(ls,ch);
- if a=1 then
- t[1] := ch
- Else
- if verif(t,k,ch) then
- begin
- k:=k+1;
- t[k]:=ch;
- end;
- end;
- calcul:=k;
- end;
- Begin
- saisie(n);
- write('There are ',calcul(n),' unique ways to climb the stairs');
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement