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

  • 相关阅读:
    检查型异常(Checked Exception)与非检查型异常(Unchecked Exception)
    maven跳过单元测试-maven.test.skip和skipTests的区别
    java JFR
    Docker常用命令
    关键字group by 、 Having的 用法
    css特效
    sql
    初识Hibernate之理解持久化类
    Hibernate 搭建
    基本 SQL 之增删改查
  • 原文地址:https://www.cnblogs.com/zzunstu/p/3406718.html
Copyright © 2011-2022 走看看