帮忙修改一段C++代码
来源:百度知道 编辑:UC知道 时间:2024/05/22 08:39:46
#include <iostream>
using namespace std;
int moves(int n)
{
int k;
if (n=0)
{
return n;
}
else if(n>0)
{
n = 2*moves(n-1)+1;
}
return n;
}
void main()
{
int i = moves(3);
cout<<i<<endl;
}
不能得到正确结果 请帮忙改正一下
using namespace std;
int moves(int n)
{
int k;
if (n=0)
{
return n;
}
else if(n>0)
{
n = 2*moves(n-1)+1;
}
return n;
}
void main()
{
int i = moves(3);
cout<<i<<endl;
}
不能得到正确结果 请帮忙改正一下
n=0 改为n==0
k好像没用
n 在递归的时候不能等于0
这条语句n = 2*moves(n-1)+1; 有问题,不知道你想得到什么值,也不知道怎么帮你改
多注意调试技巧,用下面这个一测试,你就能知道是怎么回事了:
for ( int i=0; i<10; i++)
{
cout<<moves(i)<<endl;
}