There are 100 steps, can be 1 steps across 2 steps, then a number of ways to go;
Telephone interview today. Encountered an algorithm problem, and then an instant face Meng force;
Then wit me, think smart of the thought if a person every time walk 1 steps, then the most steps, 2 steps at least step, and then obviously digress ... Fortunately, the other party in time to interrupt me ... or I guess I'm going to die on this thing . Go all the way to the black.
And then came home. Take my Mac, then think quietly, and finally write it.
var stairs = new Step ();
function Step () {
this.n1=1;
this.n2=2;
this.total=100;
This.getfunction = getfunction;
}
function GetFunction () {for
(i=2;i<this.total;i++) {
res = this.n1 + this.n2;
This.n1 = this.n2;
THIS.N2 = res;
}
return res;
}
var totalstairs = Stairs.getfunction ();
Alert (Totalstairs)
It's only 1 times. can only walk 1 steps .... Just 1 kinds .
When it's only 2, you can 1+1| | 2... 2 kinds
3 lattice time, 1+1+1| | 2+1| | 1+2 ... 3 kinds
4 Lattice Time 1+1+1+1| | 2+2| | 2+1+1| | 1+1+2| | 1+2+1 ... 5 kinds
sn = s (n-1) +s (n-2)
Fibonacci algorithm ... And then you can use
for (i=2;i<this.total;i++) {
res = this.n1 + this.n2;
This.n1 = this.n2;
THIS.N2 = res;
}
Perhaps I am not particularly good at the algorithm ~ If there are objections welcome correction
The above is small series for everyone to bring the JS face questions---On the algorithm steps of the problem of all content, I hope that we support cloud Habitat Community ~