阅读代码

这次阅读别人代码,虽然代码看上去不长,但是想要理解感觉还是挺困难的比起给自己这个题目按照自己思想写一个程序。

代码如下:

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

我个人对代码的理解:给定一个有从2到31的30数字的整数组,i=1到i=Int64.MaxValue找出i,其满足除以数组中的元素且余数不能等于0,数组中元素的下标数字相邻,遍历完数组所有数后,把i的值输出。。我是这样想的,但我实际上把代码放入程序中进行运行,编译了很长时间但结果根本出不来,我也不知道是我电脑慢还是什么原因导致的,所以无法确认我的理解是否正确,希望老师上课或者在博客中对代码进行解答!!!!(纠结ing!!!!)

posted @ 2016-03-19 16:11  朴宝英男朋友  阅读(200)  评论(2编辑  收藏  举报