您的位置:首页 > 其它

ACM练习 反素数

2015-06-06 19:47 363 查看
Problem Description
反素数就是满足对于任意i(0<i<x),都有g(i)<g(x),(g(x)是x的因子个数),则x为一个反素数。现在给你一个整数区间[a,b],请你求出该区间的x使g(x)最大。

Input
第一行输入n,接下来n行测试数据

输入包括a,b, 1<=a<=b<=5000,表示闭区间[a,b].

Output
输出为一个整数,为该区间因子最多的数.如果满足条件有多个,则输出其中最小的数.

Sample Input
3
2 3
1 10
47 359


Sample Output
2
6
240
Hint
2的因子为:1 2
10的因子为:1 2 5 10



#include <stdio.h>
#include <stdlib.h>
using namespace std;
int getFactor(int x);

int main()
{
   struct factor
   {
       int name;
       int value;
   }F;
   int n,a,b,i, x,k,c;
   bool IsFactor;
   scanf("%d", &n);
   while (n--)
   {
     scanf("%d%d", &a, &b);
     c = b-a;
     x=0;
    factor* temp = (factor*)malloc(sizeof(factor)*c);
    int* fac = (int*)malloc(sizeof(int)*b+1);
    for (i=1;i<=b;i++ )
    {
       fac[i] = getFactor(i);
    }
     for (i=a;i<=b; i++)
     {
          IsFactor = true;

     	for(k=a; k <i;k++)
     	{

    	    if ( fac[i] <= fac[k])
     	    {

     	    	IsFactor = false;
     	    	break;
     	    }
     	}
     	//printf("%d\n",IsFactor );
     	if (IsFactor)
     	{

     	    temp[x].name = i;
     	    temp[x].value = fac[i];
     	    x++;
     	}
     }
     F.value = temp[0].value;
     F.name = temp[0].name;
     for (i =1; i< x ; i++ )
     {
         if (temp[i].value >F.value)
         {
         	F.name = temp[i].name;
         	F.value = temp[i].value;
         }

     }
    printf("%d\n",F.name);
   }

    return 0;
}
int j,value;
int getFactor(int x)
{
 value = 0;
 for (j=1; j <= x; j++)
 {
 	if (x%j==0)
 	{
 		value++;
 	}
 }
    return value;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: