輸入一個正整數n,求1-1/3+1/5-1/7+......共n項之和。spa
using System; namespace _3求數列和 { class Program { static void Main(string[] args) { int num = int.Parse(Console.ReadLine()); int flag = 1; double sum = 0, item = 1; for (int i = 1; i <= num; i++) { sum += flag * (1 / item); item += 2; flag = -flag; } Console.WriteLine(sum); Console.ReadKey(); } } }