阅读代码

老师给定代码:

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

 

阅读分析:给定一个30个数字的从2至31的数组,刚开始Int64不太懂什么意思,百度查了下解释

这样就大致能理解了,通过for循环遍历数组直到结束或hit大于2之前,找到不能被整除i的数,hit就++,hit1和hit2中分别保存的是最后两个不能整除i的数组中的数,如果这两个数相邻则输出i的数值。但是运行程序,等了40分钟没出来结果。所以不知道理解的对不对还是运行出错了

 

posted @ 2016-03-19 17:02  张瑞远  阅读(379)  评论(1编辑  收藏  举报