//1. 递归求和 GetValue2(int i)
//2. 总结规律求和 GetValue(int i)
//3. 判断奇偶求和
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SumDemo2
{
class Program
{
static int GetValue2(int i)
{
int flag = 0;
if (i == 1)
return 1;
else if (i == 2)
return -1;
if (i % 2 == 0)
{
flag = 0 - GetValue2(i - 1);
}
else
{
flag = i / 2 + 1;
}
return flag;
}
static int GetValue(int i)
{
int flag = 0;
if (i == 1)
return 1;
else if (i == 2)
return -1;
if (i % 2 != 0)
{
flag = i / 2 + 1;
}
else
{
flag = 0 - i / 2 ;
}
return flag;
}
static void Main(string[] args)
{
for (int i = 1; i < 10; i++)
{
Console.Write(GetValue(i));
Console.Write(" ");
}
Console.WriteLine("
-----------------------------------------");
for (int i = 1; i < 10; i++)
{
Console.Write(GetValue2(i));
Console.Write(" ");
}
Console.WriteLine("
-----------------------------------------");
for (int i = 1; i < 10; i++)
{
int sum = 0;
for (int j = 1; j <= i; j++)
{
if (j == 1)
{
sum += j;
}
else if (j % 2 == 0)
{
sum -= j;
}
else if (j % 2 == 1)
{
sum += j;
}
}
Console.Write(sum);
Console.Write(" ");
}
Console.WriteLine("
-----------------------------------------");
Console.ReadLine();
}
}
}
