【发布时间】:2019-03-23 00:13:30
【问题描述】:
我正在解决一个问题。我想要一个返回所有方法的函数,使不同的正数int 加上等于该数字,例如 6 将是 1+5 ,2+3+1,2+4 所以将是 3
但我的解决方案返回无限循环
#include <iostream>
using namespace std;
int find(int num,int before)
{
int first=1;
int count=1;
int end =num-1;
if(end-first==0) return count;
while(end-first!=1&&end-first!=0)
{
if(end==before||first==before) continue;
first++;
end--;
}
before=first;
return count+find(end,before);
}
int main()
{
int a;
cin>>a;
int x=find(a,1);
cout<<x;
}
我在循环中尝试cout“a”并永远重复。请帮帮我。
编辑:我的代码只是解决了一个问题,所以它不是解决方案,我会尝试关闭主题,谢谢大家
【问题讨论】:
-
一旦
if(end==before||first==before) continue;命中,就没有什么可以打破循环了。 -
我的目的是防止第一个或结束等于循环,这一切,不是打破循环,谢谢你的建议
标签: c++ loops recursion while-loop