</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;
}
参考网站