A10传球游戏
2026-01-25 12:14:49
发布于:上海
9阅读
0回复
0点赞
~
#include <iostream>
#include <cstring>
#include <cstdio>
#include <iomanip>
#include <cmath>
#include <string>
#include <algorithm>
using namespace std;
long long n,m,ans;
void dfs(long long step,int id){
if (step==m){
if (id==1)ans++;
return;
}
if (id==n){
dfs(step+1,1);
}else{
dfs(step+1,id+1);
}
if (id==1){
dfs(step+1,n);
}else{
dfs(step+1,id-1);
}
return ;
}
int main() {
cin>>n>>m;
dfs(0,1);
cout<<ans;
return 0;
}
1.小练习(补全下列代码)
#include <iostream>
#include <cstring>
#include <cstdio>
#include <iomanip>
#include <cmath>
#include <string>
#include <algorithm>
using namespace std;
long long n,m,dp[50][50];//定义dp[i][j]为在第i步球在第j位的方案数
int main() {
cin>>n>>m;
dp[0][1]=1;
for(int i=1;i<=m;i++){
for (int j=1;j<=n;j++){
if (①)dp[i][j]+=dp[i-1][1];
else dp[i][j]+=②;
if (③)dp[i][j]+=dp[i-1][n];
else dp[i][j]+=④;
}
}
cout<<⑤;
return 0;
}
①
②
③
④
⑤
2.正解
#include <iostream>
#include <cstring>
#include <cstdio>
#include <iomanip>
#include <cmath>
#include <string>
#include <algorithm>
using namespace std;
long long n,m,dp[50][50];//定义dp[i][j]为在第i步球在第j位的方案数
int main() {
cin>>n>>m;
dp[0][1]=1;
for(int i=1;i<=m;i++){
for (int j=1;j<=n;j++){
if (j==n)dp[i][j]+=dp[i-1][1];
else dp[i][j]+=dp[i-1][j+1];
if (j==1)dp[i][j]+=dp[i-1][n];
else dp[i][j]+=dp[i-1][j-1];
}
}
cout<<dp[m][1];
return 0;
}

全部评论 2
就像仔仔包当年写递推作业时数组名一直用dp
5天前 来自 上海
0正确的
5天前 来自 上海
0
5天前 来自 上海
0










有帮助,赞一个