C++编程之美-数字之魅(代码清单2-38(C#代码))

代码清单2-38(C#代码)

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

               }
          }
     } 
}
发布了1224 篇原创文章 · 获赞 951 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/weixin_42528266/article/details/104027795