微软2016年校招探星夏令营第二题:最多约数问题

输入 n;找到小于 n 的数中,约数个数最多的数,如果不唯一,那末输出最小的数,这个题有两个难点我觉得,1计算约数的个数,2如何跳转,如果遍历小于n的数,那末复杂度很高,计算有重复的。下面代码,测试可用,时间很快,codeblocks+ubuntu

c plus plus

#include <iostream>
using namespace std;
int  ptr []={0,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,51};
long maxs,n,counts;
void divisors(long m,int f,int t,int pr){
    if(t>maxs||(t==maxs&&m<counts)){
	counts=m;
	maxs=t;
    }
    int j=0,l=1,nt;
    long i=m;
    while(j<pr){
	j++;
	l++;
	if(n/i<ptr[f])
	    break;
	nt=t*l;
	i=i*ptr[f];
	if(i<=n){
	    divisors(i,f+1,nt,j);
	}
    }
}
int main()
{
  maxs=-1;
  counts=-1;
   cin>>n;
   cout<<n<<endl;
   divisors(1,1,1,30);
   cout<<counts<<endl;
    return 0;
}
posted @ 2015-07-13 11:13  小兔子乖乖把门开开  阅读(265)  评论(0)    收藏  举报