Description
James enjoys shooting games. This weekend, after finishing his data structure jobs, he came to the shooting casino again. He rented a rifle from his boss and a magazine containing N bullets. Note: All bullets are loaded from the muzzle. During the shooting process, James had two options each time: getting a bullet from the clip and loading it, or hitting a bullet. Coincidentally, this Tuesday, Xiao Minggang took the chapter stack in the data structure, so, he wants to use the "stack" Data Structure to figure out how many different bullet strike sequences there are. Assume that N bullets are numbered 1, 2 ,..., N. The order of the bullets taken from the cartridge is also from 1 to n. Can you help James solve this problem?
Input
There may be multiple test inputs. The first line shows the total number of test inputs.
Each test input has only one integer n.
Output
Output an integer, that is, the total number of all different sequences.
Sample Input
Copy sample input to clipboard
13
Sample output
5
Problem Source: course exercise questions
Solution:
Method 1: formula 2n! /(N + 1) n! N !)
#include<iostream>using std::cout;using std::cin;using std::endl;int main() { long long int T, n, tmp1, tmp2, i; cin >> T; for (; T > 0; T--) { cin >> n; for (i = 2 * n, tmp1 = 1; i >= (n + 2); i--) tmp1 *= i; for (i = n, tmp2 = 1; i >= 1; i--) tmp2 *= i; cout << (tmp1 / tmp2) << endl; }}
Method 2: to be updated
(I have referenced other online materials more or less in this blog, but I have forgotten my reference for a long time. I would like to thank them !)
[Stack] Shooting Game (to be updated)