1
0
mirror of https://e.coding.net/circlecloud/Algorithm-Test.git synced 2024-11-23 02:18:52 +00:00
Algorithm-Test/Pi/Program.cs
j502647092 1c129f6a2f 提交 工程文件 ...
Signed-off-by: j502647092 <jtb1@163.com>
2015-07-08 18:54:09 +08:00

42 lines
1.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
while (true)
{
//π/4 = 1 -1/3 +1/5 -1/7 +1/9 -1/11 +……精确至少4位小数.
Console.WriteLine("请输入运算次数进行下一次计算...");
decimal a = decimal.Parse(Console.ReadLine());
Console.WriteLine("输入的次数: {0}", a);
decimal result = 0;
bool add = true;
for (decimal i = 1; i < a; i += 2)
{
//result += 1d / (2d * i - 1d) * Math.Pow(-1d, i - 1d);
if (add)
{
result += 1m / i;
add = false;
}
else
{
result -= 1m / i;
add = true;
}
//Console.WriteLine(result * 4d);
}
Console.WriteLine("PI的结果为: {0}", (result * 4m));
Console.WriteLine("PI的结果为: {0}", (result * 4m).ToString("#0.0000"));
}
}
}
}