zoukankan      html  css  js  c++  java
  • 求和:1-2+3-4+……+m

    //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(); } } }

  • 相关阅读:
    古代汉语:含义“死”的归类
    Inesa 路由器登录信息
    调试第三方源码
    暑假作业日总结
    暑假作业日总结
    暑假作业日总结
    暑假作业日总结
    暑假作业日总结
    暑假作业:《大道至简》读后感
    暑假作业日总结
  • 原文地址:https://www.cnblogs.com/zzunstu/p/3406718.html
Copyright © 2011-2022 走看看