10.求以下表达式的值,写出您想到的一种或几种实现方法: 1-2+3-4+……+m
1:
public class MainClass
{
public static void Main()
{
Console.WriteLine(Foo(30));
}
public static int Foo(int i)
{
if (i <= 0)
return 0;
else if(i > 0 && i <= 2)
return 1;
else return Foo(i -1) + Foo(i - 2);
}
}
2:
D(int m)
{
int s;
if( m % 2 == 0)
return s = D(m-1) - m
else(m % 2 != 0)
return s = D(m-1) + m
return s;
}