calcpi.cs

来自「c#的学习资料 书上的东西 很难找到的啊」· CS 代码 · 共 35 行

CS
35
字号
//
// CalcPi.cs -- Estimates pi by throwing points into a square. Use to
//              compare execution times.
//
//              Compile this program with the following command line:
//                  C:>csc CalcPi.cs
//
namespace CalcPi
{
    using System;
    class clsMain
    {
        static void Main ()
        {
            const int throws = 10000000;
            DateTime now = DateTime.Now;
            Random rand = new Random ((int) now.Millisecond);
            int Inside = 0;
            for (int i = 0; i < throws; ++i)
            {
                double cx = rand.NextDouble();
                double cy = rand.NextDouble();
                double distance = Math.Sqrt ((cx * cx) + (cy * cy));
                if (distance < 1.0)
                    ++Inside;
            }
            double pi = 4 * (double) Inside / (double) throws;
            DateTime End = DateTime.Now;
            TimeSpan Diff = End - now;
            Console.WriteLine ("pi = " + pi);
            Console.WriteLine ("Milliseconds = " + Diff.TotalMilliseconds);
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?