所以我尝试解决这个问题,但我的程序表现得很奇怪。
#include <iostream>
using namespace std;
int triangle_numbers(int n, int meh = 0)
{
int count = 0;
//calculate how many divisors there are for meh
for(int i = 1; i <= meh; i++)
if(meh%i == 0)
count++;
//if the number of divisors for meh is over 500, return meh
if(count > 500)
return meh;
//recursive call to increment n by 1 and set meh to the next triangle number
triangle_numbers(n+1, meh += n);
}
int main()
{
int cc = triangle_numbers(1);
cout << cc << endl;
}
如果我单独输出meh
,count
我会得到准确的结果,所以我不确定为什么我的程序给了我相同的数字(4246934),即使我这样做了,比如说if(count > 10)
。我有一种感觉,这可能与我的递归调用有关,但到目前为止我尝试过的一切都没有奏效。有什么帮助吗?