首先给出公式π/4 = 1 - 1/3 + 1/5 - 1/7+...,这是一个无穷级数。
我这个程序没精简,所以有质量上的问题其中的初始迭代次数和终止迭代次数需要自己看代码理解,我这里懒得介绍,才从忠县中学回来,这个学校我不多做评价,但不得不说是比忠县其他学校好。这些都是题外话。上代码。
using System;
using System.Numerics;
namespace Hello
{
class test
{
static int a;
public void Hello_()
{
Console.WriteLine("Hello, world");
}
public void StrTest(string str)
{
Console.WriteLine(str);
}
}
class Hello
{
static void printf(decimal a)
{
Console.WriteLine(a);
}
static void getchar()
{
Console.ReadLine();
}
static void Main()
{
int x = 1000;
int setup = 0;
int MareX = setup;
decimal P = 1.0M;
int move;
decimal[] PI = new decimal[x + 10];
Console.WriteLine("Code Go!");
Console.WriteLine("请输入初始迭代次数");
MareX = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入要迭代的终止次数");
x = Convert.ToInt32(Console.ReadLine());
while (x >= MareX)
{
for (int i = 1; i <= MareX; i++)
{
if (i % 2 == 0)
move = 1;
else
move = -1;
PI[i] = 1.0M / (i * 2.0M + 1) * move;
}
for (int j = 0; j <= MareX; j++)
P = P + PI[j];
Console.Write("\r迭代次数:" + MareX + " " + "计算结果" + P * 4.0M);
P = 1;
MareX++;
System.Threading.Thread.Sleep(60);
}
getchar();
}
}
}
为什么要定义一个getchar方法呢,因为我之前是C开发者,习惯了。