题解 | #牛牛学数列3#
牛牛学数列3
https://www.nowcoder.com/practice/f65c726d081c4160a9356eabf0dc21d9
public class Program { public static void Main() { double result = 1; int inPut = int.Parse(System.Console.ReadLine()); if (inPut > 1) { //观察分母得知,奇数时分母是正数,偶数时分母是负数 result = 0; for (double i = 1; i <= inPut; i++) { //如果是奇数 if (i % 2 != 0) result += 1 / i; //如果是偶数 else if (i % 2 == 0) result += 1 / (i * -1); } } System.Console.WriteLine(result.ToString("F3")); } }