• 软件工程第三周作业(2)


    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-31之间连续的两个数整除

    分析:

    Int64 :表示的是有符号的64位数,即表示值介于 -2^63 ( -9,223,372,036,854,775,808) 到2^63-1(+9,223,372,036,854,775,807 )之间的整数

    j < rg.Length:表示的是j小于数组rg的长度,即j小于30

    hit1:表示的是第一个不能被整除的数

    hit2:表示的是第二个不能被整除的数

    (hit == 2)&& (hit1+1==hit2):在2-31中上述所述的数有且只能有两个,并且这两个数是连续的

    2.这样的数不存在:

    因为这个数只能被两个数整除,所以这个数是两个数之外其他数的最小公倍数,所以先假设能被整除的两个数,算出其他数的最小公倍数,看能否整除假设的两个数,如不能,则该数为要找的最小的目标数,如能,则改变假设数继续运算。

    3.运算量较大,无法完成运算

    4.尽量减少启动其他程序,减少cpu的占用量

  • 相关阅读:
    清理disuz垃圾用户信息SQL语句
    解决C++代码单元测试中的难题不可验证和IO调用
    开源分布式平台mooon系统结构
    只显示指定网卡IP地址命令
    优秀开源杀毒软件ClamAV
    pyenv本地安装python
    TCP/IP学习笔记:ARP
    [linux] 如何在vim里面把空格缩进改为tab缩进
    TCP/IP学习笔记:ICMP
    TCP/IP学习笔记:UDP
  • 原文地址:https://www.cnblogs.com/tangxiangyi/p/5295967.html
Copyright © 2020-2023  润新知