1+2+3+4+…+n<1000
5 个解决方案
#1
#include <iostream>
using namespace std;
void solve(int &i, int sum, int Max)
{
if(sum + i >= Max)
return ;
i++;
solve(i,sum+i,Max);
}
int main()
{
int m = 1;
solve(m,0,1000);
printf("%d",m);
}
输出:
44
#2
#3
int solve(int curr, int sum, int Max)
{
return curr+sum>=Max ? curr-1 : solve(curr+1, curr+sum, Max);
}
#4
关注 接分
#5
为什么是“int &i”呢?和“int i”有什么不一样啊?
#1
#include <iostream>
using namespace std;
void solve(int &i, int sum, int Max)
{
if(sum + i >= Max)
return ;
i++;
solve(i,sum+i,Max);
}
int main()
{
int m = 1;
solve(m,0,1000);
printf("%d",m);
}
输出:
44
#2
#3
int solve(int curr, int sum, int Max)
{
return curr+sum>=Max ? curr-1 : solve(curr+1, curr+sum, Max);
}
#4
关注 接分
#5
为什么是“int &i”呢?和“int i”有什么不一样啊?