蓝桥杯 算法训练 K好数
参考:https://blog.csdn.net/jjmjeffrey/article/details/69298110
https://www.cnblogs.com/TWS-YIFEI/p/6347186.html
https://blog.csdn.net/jopus/article/details/20315381
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=105; 4 const int MOD=1e9+7; 5 int dp[N][N]={0}; 6 int main() 7 { 8 // freopen("in.txt","r",stdin); 9 int k,l; 10 cin>>k>>l; 11 for (int i=0;i<k;i++) 12 { 13 dp[1][i]=1; 14 } 15 for (int i=2;i<=l;i++) 16 { 17 for (int j=0;j<k;j++) 18 { 19 for (int c=0;c<k;c++)//for的判断应尽量简短! 20 { 21 if ((c!=j+1)&&(c!=j-1))//这里的判断条件不知为何不能放到for的判断中,望大神指教~ 22 { 23 dp[i][j]+=dp[i-1][c]; 24 dp[i][j]%=MOD;//注意取模!! 25 } 26 } 27 } 28 } 29 int ans=0; 30 for (int i=1;i<k;i++) 31 { 32 ans+=dp[l][i]; 33 ans%=MOD;//答案要注意取模!! 34 } 35 cout<<ans<<endl; 36 37 return 0; 38 }