出栈序列的统计

http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1266

View Code
 1 #include <stdio.h>  
 2 #include <string.h>  
 3 #include <math.h>  
 4 long long int a[21];  
 5 long long int zhan[21];  
 6 int main()  
 7 {  
 8     int i,j,n,t,x;  
 9     long long int s;  
10     scanf("%d",&n);  
11     memset(a,0,sizeof(a));  
12     if(n==1)  
13     {  
14         printf("1\n");  
15     }  
16     else if(n==2)  
17     {  
18         printf("2\n");  
19     }  
20     else  
21     {  
22         a[3]=1;  
23         a[2]=1;  
24         for(i=4; i<=n; i++)  
25         {  
26             for(j=1; j<=20; j++)  
27             {  
28                 zhan[j]=a[j];  
29             }  
30             memset(a,0,sizeof(a));  
31             for(j=1;j<=20;j++)  
32             {  
33                 if(zhan[j]!=0)  
34                 {  
35                     t=zhan[j];  
36                     for(x=j+1;x>=2;x--)  
37                     {  
38                         a[x]+=t;  
39                     }  
40                 }  
41             }  
42         }  
43         for(i=1,s=0; i<=20; i++)  
44         {  
45             s+=i*a[i];  
46         }  
47         printf("%lld\n",s);  
48     }  
49     return 0;  
50 }  
51    
原文地址:https://www.cnblogs.com/yelan/p/2964290.html