第三周作业二(读程序)

程序:

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:这个程序要找的是符合什么条件的数?

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

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

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

解答:

刚开始看这个程序的时候,有一些地方不太懂,这个程序使用C#写的,所以自己就建立了一个C#控制台应用程序,知道了语句Console.WriteLine是用来做输出的。利用语句Console.WriteLine("found {0}", Int64.MaxValue);将Int64.MaxValue的具体值求出来了为9223372036854775807。

函数理解:首先是第一层的for循环,i从1开始,一直到9223372036854775807,每循环一次,i增加一个数,每一次的循环都对hit,hit1,hit2进行赋值,分别为0,-1,-1,然后进入第二层for循环,j从0开始,判断条件是j小于30(即数组的长度)且hit小于等于2,判断之后如果成立,执行操作:

if ((i % rg[j]) != 0)
          {
            hit++;
            if (hit == 1)
            {
              hit1 = j;
            }
            else if (hit == 2)
            {
              hit2 = j;
            }
            else
              break;
          }

然后j加1,然后继续第二层for循环,直至不满足条件为止;然后判断hit是否等于2与hit1+1是否等于hit2的交,如果满足,那么输出i,不满足继续第一层for循环,直至i大于等于9223372036854775807为止。

1.在我的尝试演算了几个数,发现第二层循环完后所有的hit值都是3,为了验证我的想法,我在c中写了个检验的程序,

#include<stdio.h>
void main()
{
  int i;
  int a[30] =
    { 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 (int i = 1; i < 9, 223372036854775807; i++)
    {
      int hit = 0;
      int hit1 = -1;
      int hit2 = -1;
      for (int j = 0; (j < 30) && (hit <= 2); j++)
        {
          if ((i % a[j]) != 0)
          {
             hit++;
             if (hit == 1)
             {
              hit1 = j;
             }
             else if (hit == 2)
             {
               hit2 = j;
             }
             else
              break;
          }

        }
        printf("%d %d (%d %d) %d ", i,hit,hit1,hit2,hit2-hit1);
        if ((hit == 2) && (hit1 + 1 == hit2))
        {
          printf("%d ", i);
        }
    }
}

输出结果的第一列是i,第二列是hit,第三列是hit1,第四列是hit2,第四列是hit2-hit1。部分结果截图如下所示:

可能是我测试的时间太短,我测试时,第二层for循环后hit的值总是3,这样在第一层for循环中的第二次for下面的if判断就不成立,所以没有输出的i值。

虽然后来上网查了一个能够找出函数运行时间的函数,将这个加到上面那个C语言的程序中,代码如下:

#include<stdio.h>
#include < time.h >

void main()
{
   int start;
   int end;
   int time;
   int i;
   int a[30] =
    { 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 };
   start = clock();
   for (int i = 1; i < 10000000000000; i++)
   {
     int hit = 0;
     int hit1 = -1;
     int hit2 = -1;
     for (int j = 0; (j < 30) && (hit <= 2); j++)
     {
        if ((i % a[j]) != 0)
        {
          hit++;
        if (hit == 1)
        {
          hit1 = j;
        }
        else if (hit == 2)
        {
          hit2 = j;
        }
        else
          break;
        }

     }

      printf("%d %d (%d %d) %d ", i,hit,hit1,hit2,hit2-hit1);
     if ((hit == 2) && (hit1 + 1 == hit2))
     {

        printf("%d+++++++++++++++++++++++++", i);
     }

     }

  end = clock();
  time = end - start;
  printf("%d",time);
}结果没有等出来,可能是时间不够。

以上就是我对这个程序的理解,可能有错误的地方,请老师多多指导。

原文地址:https://www.cnblogs.com/shenpfei/p/5294948.html