zoukankan      html  css  js  c++  java
  • 软件工程之阅读程序,分析程序

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

      分析了下,这是一个要找1到(2^62)/2数中有多少个数是满足arg数组中除了不能被两个连续数整除的别的数都能整除的数。

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

      想了下这个需要怎么做呢,然后模拟了下,应该是将2,2,2,3,3,3,5,5,7,11,13,19,23,29,31这几个数相乘就可以的到那个数了,答案是2123581660200。不能整除的是16,17。

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

      4GHZ时钟周期就是3ns,然后我们大约估计下吧,外层循环最少循环212358166020,设为T,假设赋值,比较。。。通通是两个机器周期20T + 。。。。。大概是2个小时,不信你试试。

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

      我想最简单的应该把他分成4个,放在不同的内核上跑,效果肯定刚刚的!!

  • 相关阅读:
    no copy constructor available or copy constructor is declared &#39;explicit&#39;
    Jetty:配置连接器
    DataTable数据转换为实体
    iOS 单元測试之XCTest具体解释(一)
    UDP C/S编程
    xml的加密和解密
    lua 加密
    shell脚本加密
    实现业务系统中的用户权限管理--设计篇
    通用用户权限系统设计(数据库设计)
  • 原文地址:https://www.cnblogs.com/QuanQingli/p/5304274.html
Copyright © 2011-2022 走看看