求 1-2+3-4+5-6+7-8....M 的结果算法

 1 static void Main(string[] args)
 2 {
 3     /**
 4      * 算法题:
 5      * 求 1-2+3-4+5-6+7-8....M 的结果.
 6      * */
 7     
 8     //存储运算结果.
 9     int result = 0;
10     //记号.
11     int flag = 1;
12     //循环次数.
13     for (int i = 1; i <= 10; i++)
14     {
15         //运算结果
16         result += i * flag;
17         //调整正负值.
18         flag *= -1;
19     }
20     //输出结果
21     Console.WriteLine(result);
22     Console.Read();
23 }
原文地址:https://www.cnblogs.com/mcqueen/p/6933383.html