击鼓传花

题目链接: http://exercise.acmcoder.com/online/online_judge_ques?ques_id=719&konwledgeId=136

解题思路: f[i][j]=f[i-1][j-1]+f[i-1][j+1]; f[0][1]=1;

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int MAXN=100005;
 5 typedef long long LL;
 6 
 7 LL f[31][31];
 8 int n,m;
 9 
10 
11 int main()
12 {
13 #ifndef ONLINE_JUDGE
14     freopen("test.txt","r",stdin);
15 #endif // ONLINE_JUDGE
16     while(scanf("%d%d",&n,&m)!=-1)
17     {
18         memset(f,0LL,sizeof(f));
19         f[0][1]=1LL;
20         for (int i=1;i<=m;++i)
21         {
22             for(int j=1;j<=n;++j)
23             {
24                 f[i][j]=(f[i-1][j-1==0?n:j-1] + f[i-1][j+1==n+1?1:j+1]);
25             }
26         }
27         printf("%lld
",f[m][1]);
28     }
29     return 0;
30 }
原文地址:https://www.cnblogs.com/djingjing/p/8894136.html