[Explanations] fan ticket issue -C ++

Background topic
unprecedented football match to be held. Game tickets ticket office waiting in a long queue of fans buy tickets.

According to the provisions of the ticket, each ticket buyers purchase a ticket, and the price of each ticket is 50 yuan. There are hand-held personal N $ 50 coins in a long line of fans, and another personal handheld N 100 yuan face value of coins. Assuming that there is no change at the ticket office began selling tickets. Is this the kind of 2N how many fans queuing ticket can not find the money without embarrassing situation.

Title described
example, when n = 2, the face value of 50 yuan handheld showing fans with A, 100 represents a holding dollars fans with B. You can get up to two sets of different queuing, the conductor will not not find the money.

The first: AABB

The second: ABAB

[Programming tasks]

For a given n (0≤n≤20), calculate how many of 2N fans queuing, you can not find the ticket office will not money.

Input and output format
input format:
an integer value representing the N

Output format:
an integer representing the number of programs

Sample Input Output
Input Sample # 1:
2
Output Sample # 1:
2
Description
will open QWORD

Test: N = 15

Back: 1 second (time-out)

Analog Stack: greater than 10 minutes

Recursive algorithm: 1 seconds (time-out)

Dynamic Programming: 0 MS

Combining algorithms: 16 MS

Meaning of the questions: each 100 yuan must be preceded by a $ 50 did not go, then disappear together in situ, reminiscent of matching brackets
This question is the direction given is very clear (explain gave you tested)
Obviously, the only viable operating rules and dynamic combination algorithm.
but! Unbelievers catch fish sauce with no backtracking DFS run again feel good, 20 times out an array of group 4 group (17,18,19,20)
Code:

#include<bits/stdc++.h>
using namespace std;
int n,cnt;
void dfs(int a,int b,int step)
{
    if(b<a)return;
    if(a<0||b<0)return;
    if(step==2*n)
    {
        cnt++;
        return;
    }
    dfs(a-1,b,step+1);
    if(b>=a)dfs(a,b-1,step+1);
}
int main()
{
    cin>>n;
    dfs(n,n,0);
    cout<<cnt<<endl;
    return 0;
}

Very clear idea to be 50 or 100, non-compliance directly return, code is also very simple.
But after all, it is A not ah!
Stubborn catch fish sauce that is still not considered two possible methods given
He taught himself several Cartland ,Determined to defeat the membrane by membrane.
Use the link above essay blog of formula ②, I want to know can take a look.
I did not expect the code simpler than DFS ...
line DFS 21, this is only 11 lines
minimalism of code:

#include<bits/stdc++.h>
using namespace std;
long long h[20+2]={1,1},n;
int main()
{
    cin>>n;
    for(int i=2;i<=n;i++)
        h[i]=h[i-1]*(4*i-2)/(i+1);
    cout<<h[n]<<endl;
    return 0;
}

ov.

Guess you like

Origin www.cnblogs.com/moyujiang/p/11234332.html