English 中文(简体)
利用再保险增加奇数
原标题:Using recursion to add odd numbers
  • 时间:2009-08-15 23:45:23
  •  标签:

我正试图书写一种方法,计算所有数字奇数的总和,低于特定数字。 例如,CalcOd(7)将返回。 5 + 3 + 1 = 9 CalcOd(10)

这种方法需要采用几个分点,即第1分点,然后重新工作,在达到零之前增加所有奇数。 这是我迄今为止所做的。

    private static int CalcOdd(int n)
    {            

        if (n <= 1)
            return 1;
        else
            if (n % 2 == 0)
                n--;

        return n + CalcOdd(n - 2);
    }

它的工作做得不错,它包括增加的数目,这不是我想要的。 谁能提出更好的办法? 我也愿意把对工作的答复寄给更多的人,并增加办法,将最初通过的数字列入答复。

很多人

问题回答

为什么你们会在这里再次接触? 仅凭空;或更好,就算出数字,在简单公式中去做。

事实是,C#的确使像数学这样的东西得到极好的再入侵;此时此刻,尾声是真的。

选择:

private static int CalcOdd(int n)
{
    int sum = 0, i = 1;
    while (i < n)
    {
        sum += i;
        i += 2;
    }
    return sum;
}

你可以这样做,正如你所说的那样,但是,如果你希望更快地这样做,那么我可以告诉你,第一个奇数的总和等于零。

private static int CalcOdd(int n) {
    if (n<=1)
        return 0;

    if (n%2 == 1)
        n--;

    int k = n/2;

    return k*k;
}

工作的原因是:

哪怕是2k,其奇数为2k-1。

由于2个<代码>*1-1 = 1, 奇数低于2k。

如果是奇怪的,我们就不想把它包括在内,那么,我们就只剩下低于它的人数,而且我们自然会想做些什么。

www.un.org/Depts/DGACM/index_spanish.htm Edited to fixbreak Code.

奇数少于一定数字的总和是完美的。

(n/2)整段,使奇数少于本身。

square!

private static int CalcSumOdd(int n)
{            
    int i;
    int.tryParse(n / 2, out i);
    return i*i;
}

甚至数字:

int i = n/2;
return i*(i+1);

页: 1 上述“偶数”包括“n”的原始编号,即e fn(12) = 42 = 2 + 4 + 6 + 8 + 10 + 12 +

如果你想要排除,你要么单方面排除,要么根据参数的推移逻辑去除。

页: 1

int CalcOdd(int n)
{ 
        n--; // <----

        if (n <= 1)
            return 0; // <----
        else
            if (n % 2 == 0)
                n--;

        return n + CalcOdd(n); // <----
}

i m 此处新增加,但这似乎是一种sil然的再入侵行动,因为它可以简单地做到:

int sum(n,isEven,notFirst) {
    int c=1; //skip the else
    if (isEven) c=2;
    if (notFirst) n-=2;
    return ((n+c)*((n+c)/2))/2; }

classic discrete math sum series.. sum from 1 to 100 (odds and evens) is ((100+1)*(100/2))=5050

编辑:在我这里的法典中,如果你重新计算与否,或者反之亦然,那么现在就算不了工作,但不会把工作纳入(和法典)。 i 计票时,如7/2无纸(明显)

为什么重新发生?

private Int32 CalcOdd(Int32 value)
{
    Int32 r = 0;
    {
        while (value >= 1)
        {
            value--;
            if (value % 2 != 0)
            {
                r += value;
            }
        }               
    }
    return r;
}

使用助手功能。 CalcOdd包括检测N,看看它是否是偶然的;如果是,返回的助手(n);如果是奇的,则返回助手(n-2)。

The helper function must handle three cases: 1) n is less than 1; in this case return 0. 2) n is even, in this case return helper(n-1). 3) n is odd, in this case return n+helper(n-1).

public static int CalcOdd(int n) {
    // Find the highest even number. (Either n, or n-1.)
    // Divide that by 2, and the answer should be the square of that number.
    n = (n & 0x3FFFFFFE) >> 1;
    return (int)Math.Pow(n, 2);
}

private static int CalcOdd(int n) {
    n -= 1;
    if ((n & 1) == 0) n--;
    if (n <= 1) return 1;
    return n + CalcOdd(n - 1);
}

但是,我要说的是,做假比较好,更清洁。


private static int CalcOdd(int n) {
    int i, r = 1;
    for (i = 3; i < n; i+=2)
        r += i;
    return r;
}

由于你想要选择列入或排除第一个答案(并铭记你的“入侵”限制):

int calcOdd(int n, bool includeN)
{
    if( !includeN )
        return calcOdd(n-1, true);
    if(n<=1)
        return 1;
    else
        if(n%2 == 0)
            n--;
    return n+calcOdd(n-1, true);
}

第一类,如果获得通过,则将列入计算中。 否则,下层将开始“包括N”。

正如其他人所说的那样,这是对再入侵的无效率利用,但...... 如果你再次入侵,则审判Haskell。 它的用语几乎完全建立在这一概念之上。

int CalcOdd(int n)
{            
    n -= 1;

    if (n <= 0)
        return 0; 

    if (n % 2 == 0)
        n--;

    return n + CalcOdd(n);
}

这一职能也是休妻的,其参数使你能够决定哪怕是偶然的,而你是否想要包括第一名。 如果大家混淆了它是如何运作的,就会记住,ool也可被视为1(侵扰)和0(false)。

int Calc(int n, bool even = false, bool includeFirst = false)
{           
    n -= !includeFirst;

    if (n <= 0)
        return 0; 

    if (n % 2 == even)
        n--;

    return n + Calc(n - includeFirst, even);
}

Håkon,我已把你的法典放到2008年《联邦法典》中。

        static int Calc(int n, bool bEven, bool bIncludeFirst)
    {
        int iEven = Bool2Int(bEven);
        int iIncludeFirst = Bool2Int(bIncludeFirst);

        n -= 1 - iIncludeFirst;

        if (n <= 0)
            return 0;

        if (n % 2 == iEven)
            n--;

        return n + Calc(n - iIncludeFirst, bEven, bIncludeFirst);
    }


    private static int Bool2Int(bool b)
    {
        return b ? 1 : 0;
    }

这似乎在发挥作用。 现在,我能做什么来选择吗? i.e. 我不想让这些ool子每刻停下来......?

我将它同所有其他退款数目部分分开:(forgive the Zhu)

def sumEveryTwoRecursive(n):
    if n <= 0:
        return 0
    return n + sumEveryTwoRecursive(n - 2)

def calcOdd(n):
    return sumEveryTwoRecursive(n - (2 if n % 2 else 1))
#include <iostream>

using namespace std;

int sumofodd(int num);

int main()
{
    int number,res;
    cin>>number;
    res=sumofodd(number);
    cout<<res;
    return 0;
}
int sumofodd(int num)
{    if(num<1) return 0;
    if (num%2==0) num--;
    return num+sumofodd(num-1);
}




相关问题
热门标签