SDUT 1266 output stack sequence statistics

Source: Internet
Author: User

Topic description stack is a commonly used data structure. There are n elements waiting for the stack to be pushed to the top side of the stack, and the other side of the stack is an output stack sequence. You already know that there are two stack operations: push and pop. The former is to add an element to the stack, and the latter is to pop up the top element of the stack. Now we need to use these two operations to obtain a series of output sequences from one operation sequence. Please program to find the given n, calculate and output the sequence of numbers 1, 2 ,......, N, the total number of output sequences that may be obtained after a series of operations. Enter an integer n (1 <=n <= 15 ). Output an integer, that is, the total number of possible output sequences. Example input 3 example output 5 this is a topic of our oj. I spoke about stack today in the dormitory yesterday and wrote several questions. Sometimes I saw this question and I did not do it myself, I just wanted to do it. At first I had no idea, But I drew a picture on the paper to understand it. Regular [cpp] # include <stdio. h> # include <string. h> # include <math. h> long int a [21]; long int chan [21]; int main () {int I, j, n, m, t, x; long int s; scanf ("% d", & n); memset (a, 0, sizeof (a); if (n = 1) {printf ("1 \ n");} else if (n = 2) {printf ("2 \ n");} else {a [3] = 1; a [2] = 1; for (I = 4; I <= n; I ++) {for (j = 1; j <= 20; j ++) {chan [j] = a [j];} memset (a, 0, sizeof (a); for (j = 1; j <= 20; j ++) {if (chan [j]! = 0) {t = chan [j]; for (x = j + 1; x> = 2; x --) {a [x] + = t ;}}}} for (I = 1, s = 0; I <= 20; I ++) {s + = I * a [I];} printf ("% lld \ n ", s);} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.