讀程式回答問題

syhqqq發表於2016-03-19

問題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);
        }
      }
    }
  }
}

問題1:要找的是不能被第i和第i+1整除的數,但能被其他數整除

問題2:我的電腦反正是沒執行處結果,應該存在吧,最小的數不知道

問題3:執行了但沒有結果

問題4:在多核cpu下使用,增大cpu的的主頻

相關文章