最多约数问题

</pre><p><span style="font-family:Arial; line-height:26px"><strong><span style="font-size:24px">问题描述: </span></strong></span></p><p><span style="font-family:Arial; line-height:26px"><strong><span style="font-size:24px"></span></strong></span></p><span style="font-family:Arial; font-size:18px; line-height:26px">正整数x的约数是能整除x的正整数。正整数x 的约数个数记为div(x)。例如,1,2,5,10 都是正整数10 的约数,且div(10)=4。设a 和b 是2 个正整数,a≤b,找出a和b之间约数个数最多的数x。 </span><br style="font-family:Arial; font-size:18px; line-height:26px" /><span style="font-family:Arial; font-size:18px; line-height:26px">编程任务: </span><br style="font-family:Arial; font-size:18px; line-height:26px" /><span style="font-family:Arial; font-size:18px; line-height:26px">对于给定的2个正整数a≤b,编程计算a 和 b 之间约数个数最多的数。 </span><br style="font-family:Arial; font-size:18px; line-height:26px" /><span style="font-family:Arial; font-size:18px; line-height:26px">数据输入: </span><br style="font-family:Arial; font-size:18px; line-height:26px" /><span style="font-family:Arial; font-size:18px; line-height:26px">输入数据由文件名为input.txt的文本文件提供。文件的第1 行有2 个正整数 a和 b。 </span><br style="font-family:Arial; font-size:18px; line-height:26px" /><span style="font-family:Arial; font-size:18px; line-height:26px">结果输出: </span><br style="font-family:Arial; font-size:18px; line-height:26px" /><p><span style="font-family:Arial; font-size:18px; line-height:26px">程序运行结束时,找到a 和b之间约数个数最多的那个数及最多约数个数。</span></p><p><span style="font-family:Arial; line-height:26px"><strong><span style="font-size:24px"></span></strong></span></p><p><span style="font-family:Arial; line-height:26px"><strong><span style="font-size:24px"></span></strong></span></p><p><span style="font-family:Arial; line-height:26px"><strong><span style="font-size:24px">问题剖析:</span></strong></span></p><p><span style="font-family:Arial; line-height:26px"><strong><span style="font-size:24px"></span></strong></span></p><p><span style="font-family:Arial; line-height:26px"><span style="font-size:18px"><span style="font-family:Arial; line-height:26px"> 主要数学原理:若一个数N满足:N = A1</span><sup style="font-family:Arial; line-height:26px">N1</sup><span style="font-family:Arial; line-height:26px"> * A2</span><sup style="font-family:Arial; line-height:26px">N2</sup><span style="font-family:Arial; line-height:26px"> * A3</span><sup style="font-family:Arial; line-height:26px">N3</sup><span style="font-family:Arial; line-height:26px"> * …… * Am</span><sup style="font-family:Arial; line-height:26px">Nm</sup><span style="font-family:Arial; line-height:26px">,则n的约数个数为(N1 + 1) (N2 + 1) (N3 + 1) …… (Nm + 1)。这是可以用乘法原理证明的。</span></span></span></p><p><span style="font-family:Arial; line-height:26px"><span style="font-size:18px"><span style="font-family:Arial; line-height:26px"></span></span></span></p><p><span style="font-family:Arial; line-height:26px"><span style="font-size:18px"><span style="font-family:Arial; line-height:26px"></span></span></span></p><p><span style="font-family:Arial; line-height:26px"><span style="font-family:Arial; line-height:26px"><strong><span style="font-size:24px">代码分析:</span></strong></span></span></p><p><span style="font-family:Arial; font-size:18px; line-height:26px"></span></p><pre name="code" class="cpp">#include <iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <algorithm>
#define MAXP 1000000

using namespace std;

bool get[MAXP+1];
int prime[MAXP];
int cnt;

//筛选法打印素数表
void Initprime()
{

    get[0]=get[1]=false;
    for (long i = 2; i <= MAXP; i++)
        get[i] = true;

    for (long i = 2; i <= MAXP; i++)
        if (get[i])
        {
            long j = i + i;
            while (j <= MAXP)
            {
                get[j] = false;
                j += i;
            }
        }

    long j=0;
    prime[0]=0;
    for (long i = 2; i <= MAXP; i++)
        if (get[i]) prime[++j] = i; //!注意prime[0]中没有存数
    cnt= j;
}

//求数m的因子个数
int Div( int m )
{
    int tmp,ret=1;
    if( m<=MAXP && get[m] ) //如果m是MAXP内的质数
        return 2;
    for( int i=1; prime[i]*prime[i] <= m && i<cnt; i++ )  //!prime[]是从下标1开始的。     
      <span style="white-space:pre">						</span>//若一个整数n有一个大于sqrt(n)的因子,则n的其他因子必然小于sqrt(n),且大于sqrt(n)的因子最多只有一个。
    {
        if( m % prime[i] == 0 )
        {
            tmp = 0;
            while( m % prime[i] == 0 )
            {
                tmp ++;
                m /= prime[i];
            }
            ret = ret * ( tmp+1 );//(N1+1)*(N2+2)*(N3+3)*(N4+4)*...*(Nn+n)
        }
    }

    if( m != 1 )
        ret = ret * 2;

    return ret;
}

int main( )
{
    int m, n;
    int max, ret;
    Initprime( );
    while( scanf( "%d%d", &m, &n ) != EOF )
    {
        max = 1;
        int num=0;
        for( int i=m; i<=n; i++ )
        {
            ret = Div( i );
            if( ret > max )
            {
                max = ret;
                num = i;
            }

        }
//        cout<<"约数最多的是 "<<num<<" ,个数为 "<<max<<endl;
        cout<<max<<endl;
    }
    return 0;
}


参考网站

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张之海

若有帮助,客官打赏一分吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值