Question: [] queue up for tickets

Title Description
there are M kids to the park to play, tickets are $ 1. Wherein the child band money N $ 1, K child with money of $ 2. The conductor did not change, ask how many of these kids line up a total that was open conductor always looking for change. Note: Two children take a yuan change, swap their position, can be considered a new row method. (M <= 10)

Entry

Input line, M, N, K (where M = N + K, M <= 10).

Export

Output line, the total queuing scheme.

Sample input

4 2 2

Sample Output

8

First, we can know:
1. If the holding is less than $ 1 children count the number of children held $ 2, then it is surely no solution is.

2. If when M == 0, i.e., no one to buy a ticket, is no solution of.

3. Therefore, only discussion N> = where K (affirmative solvability): the
paper through a large number of calculations, a rule can be summarized to give:

Without taking into account ( two kids get a yuan change, swap their position, can be considered a new row method. When) this case, we can get the result result arranged by recursively.

But the problem requires us to consider that situation, which we then calculate N, K his full array, (ie N! And K!), And then multiplied by the result.

The end result ( Result X N! X K! ) Is directly output it.

code show as below:

#include<iostream>
#include<algorithm>
#include<vector>
#include<bits/stdc++.h>

using namespace std;
int paixu_num = 0;

void getNUM(int remain_N, int remain_K, int linQian) {//remain_N  持有1元的数   remain_K  持有2元的数   linQian  1元零钱数
	if (remain_N == 0 || remain_K == 0) {//其中一个为零后,剩余那种直接摆在末尾就行了
		paixu_num++;
		return;
	}
	else if (linQian > 0) {
		getNUM(remain_N - 1, remain_K, linQian + 1);
		getNUM(remain_N, remain_K - 1, linQian - 1);
	}
	else {//零钱没有了,只能先让1元的孩子买票,使得售票员获得零钱
		getNUM(remain_N - 1, remain_K, linQian + 1);
	}
}
int pl(int n) {//得到n的阶乘
	int num = 1;
	for (int i = 1; i <= n; i++) {
		num *= i;
	}
	return num;
}
int main() {
	int M, N, K;		//N-1,K-2,M = N+K<=10
	cin >> M >> N >> K;
	if (N < K || M == 0) {// 两种无解的情况  要先排除
		cout << 0;
	}
	else {//因为上面排除N<K的情况了,所以肯定有解
		getNUM(N, K, 0);
		cout << paixu_num * pl(N) * pl(K);
	}
	return 0;

}




Published 35 original articles · won praise 0 · Views 650

Guess you like

Origin blog.csdn.net/enjoy_code_/article/details/104501067