方法一:
public static int Sum(int m)
{
int sum = 0;
bool bol = true;
for (int i=1;i<=m;i++)
{
if (bol == true)
{
sum += i;
}
else
{
sum -= i;
}
bol = !bol;
}
return sum;
}
方法二:
public static int Sum(int m)
{
int sum = 0;
for (int i = 1; i <=m; i++)
{
if (i % 2 == 0)
{
sum -= i;
}
else { sum += i; }
}
return sum;
}