zoukankan      html  css  js  c++  java
  • c#代码阅读

    问题1:这个程序要找的是符合什么条件的数?

    问题2:这样的数存在么?符合这一条件的最小的数是什么?

    问题3:在电脑上运行这一程序,你估计多长时间才能输出第一个结果?时间精确到分钟(电脑:单核CPU 4.0G Hz,内存和硬盘等资源充足)。

    问题4:在多核电脑上如何提高这一程序的运行效率?

    using System;

    using System.Collections.Generic;

    using System.Text;

    namespace FindTheNumber

    {
      class Program
      {
        static void Main(string[] args)
        {
          int [] rg =
              {2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,
               20,21,22,23,24,25,26,27,28,29,30,31};
          for (Int64 i = 1; i < Int64.MaxValue; i++)
          {
            int hit = 0;
            int hit1 = -1;
            int hit2 = -1;
            for (int j = 0; (j < rg.Length) && (hit <=2) ; j++)
            {
              if ((i % rg[j]) != 0)
              {
                hit++;
                if (hit == 1)
                {
                  hit1 = j;
                }
                else if (hit == 2)
                {
                  hit2 = j;
                }
                else
                  break;
              }

            }
            if ((hit == 2)&& (hit1+1==hit2))
            {
              Console.WriteLine("found {0}", i);
            }
          }
        }
      }
    }

    代码分析过程

    由于没有学过C#,所以用所学的C/C++知识来理解C#固然不是一件容易的事。所以我在百度上查了很多资料。

    但是也是大概明白~~

    1.这个程序主要是找到一个不能被 2~31 中相邻的两个数整除,但可以被其余28个数整除。
    2.23*33*52*7*11*13*19*23*29*31=2123581660200,相邻的两个数为16 17;
    3.运行不出来啊!
    4.减少其他程序的运行,减少外界设备的连接。既然是多核电脑,多线程处理程序的能力会比单核的会更强,但是其他程序的运行和外界设备的连接依然会有一定的影响。

  • 相关阅读:
    mongodb
    python中读取文件的read、readline、readlines方法区别
    uva 129 Krypton Factor
    hdu 4734
    hdu 5182 PM2.5
    hdu 5179 beautiful number
    hdu 5178 pairs
    hdu 5176 The Experience of Love
    hdu 5175 Misaki's Kiss again
    hdu 5174 Ferries Wheel
  • 原文地址:https://www.cnblogs.com/lwl123/p/5372162.html
Copyright © 2011-2022 走看看