Description
During physical education classes, Tom often plays games with his classmates. This time, the teacher played a pass game with his classmates.
The game rules are as follows: n students stand in a circle, and one of them holds a ball in his hand. When the teacher starts to pass the ball, each student can pass the ball to one of the two students (either left or right). When the teacher plays the whistle again, the pass is stopped. At this time, the student holding the ball but not passing it out is the loser, I want to show you a program.
Clever Tom raised an interesting question: how many different pass methods can be used to transfer the ball from the very young man's hand. After passing the ball m times, he returned to Tom. The two pass passing methods are regarded as different methods. if and only when the two methods are used, the students who receive the ball have different sequences in the order of the ball. For example, there are three students on the first, second, and third, and assume that Tom is the first, after passing the ball three times back to Tom, there are two methods: 1-> 2-> 3-> 1 and 1-> 3-> 2-> 1.
Input
Each line has two integers n, m (3 <=n <=30,1 <= m <= 30) separated by spaces ). The input end with 0.
Output
Each row has an integer indicating the number of methods that match the meaning of the question.
Solution: it is easy to know from the question that each step of the pass process will affect the subsequent results. Therefore, you need to create tables like dynamic planning. S [I] [J] indicates the number of items that are passed to the J-person after I pass operations (considering that the array is actually a J-1 since 0 ). Because it is left and right, and in a circle, therefore, s [I] [J] = s [I-1] [J-1] + s [I-1] [J + 1]; finally, you only need to output s [m] [0], because Tom is number 1.
#include <iostream> using namespace std;int main(){ int s[35][35]={0}; int i , j, k , m , n; s[0][0]=1; cin >> n >> m; while(n!=0&&m!=0) { for (i = 1; i <= m; i++) { s[i][0]=s[i-1][n-1]+s[i-1][1]; for (j = 1; j < n-1; j++) s[i][j]=s[i - 1][j - 1] + s[i - 1][j + 1]; s[i][n - 1]=s[i - 1][n - 2]+s[i - 1][0]; } cout << s[m][0] << endl; cin >> n >> m;}}