コード例 #1
0
        // 将 a[i] + a[j] + a[k] 改为 (long)a[i] + a[j] + a[k] 即可。
        // 此时整个式子将按照精度最高(也就是 long)的标准计算。
        static void Main(string[] args)
        {
            int[] a = new int[4];
            a[0] = int.MaxValue;
            a[1] = int.MaxValue;
            a[2] = int.MinValue;
            a[3] = 1;

            Console.WriteLine(ThreeSum.Count(a));//2
        }
コード例 #2
0
    /// <summary>
    /// 返回对 n 个随机整数的数组进行一次 ThreeSum 所需的时间。
    /// </summary>
    /// <param name="n">随机数组的长度。</param>
    /// <returns>运行时间,单位为毫秒。</returns>
    public static double TimeTrial(int n)
    {
        var a      = new int[n];
        var random = new Random(DateTime.Now.Millisecond);

        for (var i = 0; i < n; i++)
        {
            a[i] = random.Next(-MaximumInteger, MaximumInteger);
        }
        var timer = new Stopwatch();

        ThreeSum.Count(a);
        return(timer.ElapsedTime());
    }
コード例 #3
0
 // 由于数组已经排序(从小到大),负数在左侧,正数在右侧。
 // TwoSumFaster
 // 设最左侧下标为 lo,最右侧下标为 hi。
 // 如果 a[lo] + a[hi] > 0, 说明正数太大,hi--。
 // 如果 a[lo] + a[hi] < 0,说明负数太小,lo++。
 // 否则就找到了一对和为零的整数对,lo++, hi--。
 //
 // ThreeSumFaster
 // 对于数组中的每一个数 a,ThreeSum 问题就等于求剩余数组中所有和为 -a 的 TwoSum 问题。
 // 只要在 TwoSumFaster 外层再套一个循环即可。
 static void Main(string[] args)
 {
     char[] split = new char[1] {
         '\n'
     };
     string[] testCases = TestCase.Properties.Resources._1Kints.Split(split, StringSplitOptions.RemoveEmptyEntries);
     int[]    a         = new int[testCases.Length];
     for (int i = 0; i < testCases.Length; i++)
     {
         a[i] = int.Parse(testCases[i]);
     }
     Array.Sort(a);
     Console.WriteLine(TwoSum.Count(a));
     Console.WriteLine(TwoSumFaster(a));
     Console.WriteLine(ThreeSum.Count(a));
     Console.WriteLine(ThreeSumFaster(a));
 }
コード例 #4
0
        private static void ThreeSumCalc(int count)
        {
            var arr   = ThreeSum.GetRandomArray(count);
            var watch = new Stopwatch();

            watch.Start();
            var result = ThreeSum.Count(arr);

            watch.Stop();
            var ts = watch.Elapsed;

            System.Console.WriteLine();
            System.Console.WriteLine($"消耗时间: {ts.TotalMilliseconds} ms ({ts.TotalSeconds} s)");
            System.Console.WriteLine($"问题规模: {arr.Length}");
            System.Console.WriteLine($"求解结果(三个数和为 0 的个数): {result}");
            System.Console.WriteLine();
        }
コード例 #5
0
    /// <summary>
    /// 返回对 n 个随机整数的数组进行一次 ThreeSum 所需的时间。
    /// </summary>
    /// <param name="n">随机数组的长度。</param>
    /// <param name="repeatTimes">重复测试的次数。</param>
    /// <returns></returns>
    public static double TimeTrial(int n, int repeatTimes)
    {
        var    a      = new int[n];
        double sum    = 0;
        var    random = new Random(DateTime.Now.Millisecond);

        for (var i = 0; i < n; i++)
        {
            a[i] = random.Next(-MaximumInteger, MaximumInteger);
        }
        for (var i = 0; i < repeatTimes; i++)
        {
            var timer = new Stopwatch();
            ThreeSum.Count(a);
            sum += timer.ElapsedTime();
        }
        return(sum / repeatTimes);
    }
コード例 #6
0
        /// <summary>
        /// 返回对 n 个随机整数的数组进行一次 ThreeSum 所需的时间。
        /// </summary>
        /// <param name="n">随机数组的长度。</param>
        /// <param name="repeatTimes">重复测试的次数。</param>
        /// <returns></returns>
        public static double TimeTrial(int n, int repeatTimes)
        {
            int[]  a      = new int[n];
            double sum    = 0;
            Random random = new Random(DateTime.Now.Millisecond);

            for (int i = 0; i < n; i++)
            {
                a[i] = random.Next(-MAXIMUM_INTEGER, MAXIMUM_INTEGER);
            }
            for (int i = 0; i < repeatTimes; i++)
            {
                Stopwatch timer = new Stopwatch();
                ThreeSum.Count(a);
                sum += timer.ElapsedTime();
            }
            return(sum / repeatTimes);
        }
コード例 #7
0
ファイル: Program.cs プロジェクト: hlzn/datastruct
        static void Main(string[] args)
        {
            var       ints  = In.ReadInts("1Kints.txt").ToArray();
            Stopwatch watch = new Stopwatch();

            watch.Start();
            var triplets = ThreeSum.Count(ints);

            watch.Stop();
            Console.WriteLine(String.Format("The number of zero sum triplets is {0} for 1Kints, process performed in {1} seconds.", triplets, watch.Elapsed));

            ints  = In.ReadInts("2Kints.txt").ToArray();
            watch = new Stopwatch();
            watch.Start();
            triplets = ThreeSum.Count(ints);
            watch.Stop();
            Console.WriteLine(String.Format("The number of zero sum triplets is {0} for 2Kints, process performed in {1} seconds.", triplets, watch.Elapsed));

            ints  = In.ReadInts("4Kints.txt").ToArray();
            watch = new Stopwatch();
            watch.Start();
            triplets = ThreeSum.Count(ints);
            watch.Stop();
            Console.WriteLine(String.Format("The number of zero sum triplets is {0} for 4Kints, process performed in {1} seconds.", triplets, watch.Elapsed));

            ints  = In.ReadInts("8Kints.txt").ToArray();
            watch = new Stopwatch();
            watch.Start();
            triplets = ThreeSum.Count(ints);
            watch.Stop();
            Console.WriteLine(String.Format("The number of zero sum triplets is {0} for 8Kints, process performed in {1} seconds.", triplets, watch.Elapsed));

            ints  = In.ReadInts("16Kints.txt").ToArray();
            watch = new Stopwatch();
            watch.Start();
            triplets = ThreeSum.Count(ints);
            watch.Stop();
            Console.WriteLine(String.Format("The number of zero sum triplets is {0} for 16Kints, process performed in {1} seconds.", triplets, watch.Elapsed));


            Console.ReadLine();
        }
コード例 #8
0
        public void FastVsSlow(string file, int expectedCount, int timeoutInSeconds)
        {
            string actualFilePath = $"../../data/{file}";

            if (!File.Exists(actualFilePath))
            {
                throw new InvalidOperationException(file);
            }

            int[] intArray = File.ReadAllLines(actualFilePath).Select(s => int.Parse(s)).ToArray();

            var fast = Helper.Time(() => ThreeSumFast.Count(intArray), "3SumFast");

            Assert.Equal(expectedCount, fast.Item1);

            var slow = Helper.Time(() => ThreeSum.Count(intArray), "3SumSlow");

            Assert.Equal(expectedCount, slow.Item1);

            Assert.True(fast.Item2 < slow.Item2);
            Assert.True(fast.Item2 + slow.Item2 < timeoutInSeconds * 1000);
        }
コード例 #9
0
ファイル: Program.cs プロジェクト: dozer111/fofanov_algorytms
        // самые первые занятия, когда показывался подсчет времени
        private static void TestTimeComplexityOnKints()
        {
            /// 1 => 160ms
            /// 2 => 1.35s
            /// 4 => 10.26s
            /// 8 => 1m20s
            ///
            var ints = In.ReadInts("4Kints.txt").ToArray();

            var watch = new Stopwatch();

            watch.Start();

            var triplets = ThreeSum.Count(ints);

            watch.Stop();

            Console.WriteLine($"The number of \"zero-triplets\" : {triplets}");
            Console.WriteLine($"Time taken: {watch.Elapsed:g}");


            Console.Read();
        }