一道题,求检验!
C#:
1.计算s=1+(1+2)+(1+2+3)+……+(1+2+3+……+n)的值,当某一项的值超过50时,结束循环,并将当时的s的值进行输出,将当时刚好超过50的那一项的值也输出
for (int i = 1; xh ; i++)
{
jd = 0;
for (int k = 1; k <= i; k++)
{
jd = jd + k;
}
jg = jg + jd;
if (jd>=50)
{
xh = false;
}
}
------解决方案--------------------
C# code
static int Fun(int _i, int _sum)
{
int sum = 0;
for (int j = 0; j <= _i; j++)
{
sum = sum + j;
}
sum = sum + _sum;
if (_i == 50)
{
return sum;
}
return Fun(_i + 1, sum);
}
int x=Fun(1,0)