Jumping Step problem
Topic:
A step has a total of n-level, if you can jump 1, you can jump 2 level.
Find out how many total jumps are in total and analyze the time complexity of the algorithm.
Analysis:
It is also a relatively basic topic, which can be easily solved by recursion.
The code is implemented as follows (GCC compiles through):
#include "stdio.h"
#include "stdlib.h"
int function (int n);
int main (void)
{
int tmp;
TMP = function (5);
printf ("%3d\n", TMP);
return 0;
}
int function (int n)
{
if (n = 1) return
1;
else if (n = = 2) return
2;
else return
function (n-1) + function (n-2);
}
Joseph Ring Problem
topic:
N Numbers (0,1,..., n-1) Form a circle, starting with the number 0, each time from the circle to remove the number m (the first is the current number itself, the second is the next number of the current number). When a number is deleted, the number of m digits continues to be deleted from the next number that is deleted. Ask for the last digit left in this circle.
(In fact, so much is the question of Joseph Ring)
Analysis:
Before the study of the linked list also saw Joseph ring problem, at that time is to take the cycle chain table simulation of the whole process to solve, today on the Internet to see an analysis. Write down:
The title asks for the last remaining number (expressed in the final), which is the number of the numbers, and the position of the (0,1,..., n-1). Clear the information in the topic, so we have to sum up the number. If you know the position of this number in the remaining K number, how to find its position in the number of remaining k+1, this step-by-step derivation of its position in the number of N, that is, the request. Why this can be summed up, because this last remaining number in all the deletion process survived to survive, but each time the deletion of a number, its position is changed, know finally, its position is 0 (only one number left).
Now to analyze the first number after the deletion, the position of the last number before what the relationship. In these n digits, the first number that is deleted is (m-1)%n, which is written as K for simplicity. Then the number of n-1 left after the deletion of K is 0,1,..., k-1,k+1,..., n-1, and the next start Count is k+1. Equivalent to the rest of the sequence, k+1 to the front, thus forming a sequence k+1,..., n-1,0,... k-1.
K+1-> 0
K+2-> 1
...
N-1-> n-k-2
0-> N-k-1
...
K-1-> n-2
Now that we know where the last position is when there is a n-1 number, which is written as F (n-1,m), how do you get the relationship between F (n,m) about F (n-1,m)? Expressed in X,y, as follows:
Y X
K+1-> 0
K+2-> 1
...
N-1-> n-k-2
0-> N-k-1
...
K-1-> n-2
Y= (x+k+1)%n
K = (m-1)%n
So y= (x+m)%n, the final relationship is as follows:
0 N=1
F (n,m) ={
[F (n-1,m) +m]%n n>1
According to the relationship can be very convenient to get the code
The code implementation is as follows:
int lastremaining (int n, int m)
{
if (N < 1 | | m < 1)
return-1;
int last = 0;
for (int i = 2; I <= n; i + +) the last
= (last + m)% i;
return to last;
}