C# 使用递归添加奇数
我试图写一个方法来计算所有小于给定数的数的奇数之和。例如,CalcOdd(7)将返回5+3+1=9。CalcOdd(10)将返回9+7+5+3+1=25等 该方法需要接受一个数字,减去1,然后递归地将所有奇数相加,直到它达到0。这就是我目前所拥有的C# 使用递归添加奇数,c#,recursion,C#,Recursion,我试图写一个方法来计算所有小于给定数的数的奇数之和。例如,CalcOdd(7)将返回5+3+1=9。CalcOdd(10)将返回9+7+5+3+1=25等 该方法需要接受一个数字,减去1,然后递归地将所有奇数相加,直到它达到0。这就是我目前所拥有的 private static int CalcOdd(int n) { if (n <= 1) return 1; else
private static int CalcOdd(int n)
{
if (n <= 1)
return 1;
else
if (n % 2 == 0)
n--;
return n + CalcOdd(n - 2);
}
private static int CalcOdd(int n)
{
如果(n为什么在这里使用递归?只需循环;或者更好,在一个简单的方程中计算出数学来实现它
事实上,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)
{
整数和=0,i=1;
而(i
私有静态int-CalcOdd(int-n){
n-=1;
如果((n&1)==0)n--;
如果(n为什么使用递归
private Int32 CalcOdd(Int32 value)
{
Int32 r = 0;
{
while (value >= 1)
{
value--;
if (value % 2 != 0)
{
r += value;
}
}
}
return r;
}
小于给定数的奇数之和是一个完全平方
求(n/2)的整部分,使奇数的个数小于其本身
算了,瞧
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);
更正。上述“偶数和”包括原始数字“n”。即fn(12)=42=2+4+6+8+10+12
如果要排除它,应该单方面排除它,或者根据传入的参数使用逻辑删除它。使用帮助函数。CalcOdd包括测试n以查看它是偶数还是奇数;如果是偶数,则返回帮助器(n);如果是奇数,则返回帮助器(n-2)
助手函数必须处理三种情况:
1) n小于1;在本例中,返回0。
2) n是偶数,在本例中为返回帮助器(n-1)。
3) n是奇数,在本例中,返回n+helper(n-1)。您可以使用您所说的递归来执行此操作,但是如果您希望更快地执行此操作,那么我可以告诉您,n个前奇数的总和等于n*n
private static int CalcOdd(int n) {
if (n<=1)
return 0;
if (n%2 == 1)
n--;
int k = n/2;
return k*k;
}
private static int CalcOdd(int n){
如果(nintcalcodd(intn)
{
n-=1;
如果(n这里有一个修正
int CalcOdd(int n)
{
n--; // <----
if (n <= 1)
return 0; // <----
else
if (n % 2 == 0)
n--;
return n + CalcOdd(n); // <----
}
intcalcodd(intn)
{
n--;//因为您想要包含或排除第一个答案的选项(并且记住“递归”约束):
int-calcOdd(int-n,bool-includeN)
{
如果(!包括)
返回calcOdd(n-1,真);
如果(nHåkon,我已经将您的代码移植到VS2008中的c#中,如下所示
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;
}
static int Calc(int n、bool bEven、bool bIncludeFirst)
{
int iEven=Bool2Int(bEven);
int IIINCLUDEFIRST=Bool2Int(bIncludeFirst);
n-=1-包括第一类;
如果(n我是新来的,但这似乎是一个愚蠢的递归练习,因为它可以用一个简单的方程来完成:
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; }
经典离散数学和数列。。
从1到100(赔率和偶数)的总和为((100+1)*(100/2))=5050
编辑:在我这里的代码中,如果你计算n为偶数的赔率之和,或者n为偶数的赔率之和,那么它是不起作用的,但我现在不打算把这项工作放在其中(并对代码进行倾斜)。我假设你的代码在命中函数时会处理好这一点。例如,7/2不是int(显然)我要将“设为奇数”部分与“每隔一个降序数求和”部分分开:(请原谅Python)
def SUMEVERYTWERCURSIVE(n):
如果仅仅因为这里还没有,我就决定在这个钉子上使用LINQ锤子
(借用Nick D和Jason的配对编程答案)
void Main()
{
GetIterator(7,true,false).Sum().Dump();
//返回9
GetIterator(10,true,false).Sum().Dump();
//返回25
}
公共IEnumerable GetIterator(int n,bool isOdd,bool includeOriginal)
{
如果(包括原始)
n++;
如果(isOdd)
返回GetIterator(n,1);
其他的
返回GetIterator(n,0);
}
公共IEnumerable GetIterator(int n,int奇数)
{
n--;
if(n<0)
屈服断裂;
如果(n%2==奇数)
收益率n;
foreach(GetIterator中的int i(n,奇数))
收益率i;
}
#包括
使用名称空间std;
int sumofodd(int num);
int main()
{
整数;
cin>>数量;
res=sumofodd(数字);
“你为什么要在这里使用递归?”也许他的教授想让他使用递归;)非常感谢Marc,我意识到了循环选项,但想用递归取乐。它提供了一些递归挑战,因为它是有条件的(只有奇数)它需要在第一个过程中减去1,但在随后的过程中不减去1。为什么要使用递归而不是简单的数学公式呢?除非我遗漏了什么,否则这对生产代码毫无意义。噢,让我们用LINQ-y goodness:private static int CalcOdd(int n){return(从可枚举范围(0,n)中的x开始),其中(x&1)==1选择x.Sum();}
inti=n/2;
?为什么要使用TryParse
?这是一种获取“n”的一半的整个部分的笨拙方法.也许有一种更有效的方法来获取C#中的值,它甚至不会编译。但是,呃,请不要TryParse
!你在做整数除法,所以它完全没有必要。嗨,Joshua,请看我上面关于希望使用递归的评论,我也希望能够将其移植到即使是no,也可以选择包含或者排除答案中的原始数字。例如,calcodd(4)->4+3+1或calcodd(4)->3+1使用TryParse甚至不起作用,因为您需要提供一个字符串作为第一个参数。对于整数,n/2将给出完整的数字,对于其他所有数字,您都可以使用Math.Truncate()。您应该调用calcodd(n)
,因为你在函数调用开始时减去了一。这是错误的。啊,这将是数学位;-p我从一张纸上开始,和(1…n)=n(n+1)/2,但还没有到最后;-pIt很明显,如果你看1,1+3,1+3+5,…)-1他知道
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);
}
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);
}
int CalcOdd(int n)
{
n--; // <----
if (n <= 1)
return 0; // <----
else
if (n % 2 == 0)
n--;
return n + CalcOdd(n); // <----
}
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);
}
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;
}
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; }
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))
void Main()
{
GetIterator(7, true, false).Sum().Dump();
// Returns 9
GetIterator(10, true, false).Sum().Dump();
// Returns 25
}
public IEnumerable<int> GetIterator(int n, bool isOdd, bool includeOriginal)
{
if (includeOriginal)
n++;
if (isOdd)
return GetIterator(n, 1);
else
return GetIterator(n, 0);
}
public IEnumerable<int> GetIterator(int n, int odd)
{
n--;
if (n < 0)
yield break;
if (n % 2 == odd)
yield return n;
foreach (int i in GetIterator(n, odd))
yield return i;
}
#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);
}