因子数Java版

我们引进题目来进行讲解:

因子个数

首先我们定义一个数x的因子个数为f(x),例如f(6)=4,即{1,2,3,6}; 现在给你一个区间[l,r],求区间中f(x)最大的数为多少,输出x。 注意:当多个值含有相同的因子个数时,输出x最小的值。

Input

样例组数为T组,之后每行两个整数[l,r]表示区间长度范围。

1<=l<=r<=5000.

Output

输出为一个整数,为满足题意的最小x值.

Sample Input

3

1 20

100 300

30 30

Sample Output

12

240

30

题意:获得一个区间内因子个数最多的一个数

解题关键:了解如何求得一个非零整数的因子数

因子概念:两个非零整数相乘得n,则这两个数均为n的因子

一般思路是从1-n历遍,若n%i==0,这i和n/i均为该数的因子;这里我们用一个简化的写法:

static int getFactors(int n) {//获得因子数
            int count = 0;
            if (n == 0)
                return count;
            else {
                for (int i = 1; i <= Math.sqrt(n); i++) {
/*只用计算1到n的平方根之间的数,这是因为当我们得出n % i == 0时,同时确定了i和n/i两个因子,不用重复计算.
举个栗子:64的因子:1,2,4,8,16,32,64   64的平方根8相当于一个分界线的作用*/
                    if (n % i == 0) {
                        if (i != n / i) {//考虑n=1的特殊情况
                            count += 2;
                        } else {
                            count += 1;
                        }
                    }
                }
                return count ;
            }
        }

说到这里问题其实已经迎刃而解了

这里我们之间放上源码(输入用到的是快输写法):

    import java.io.*;

    public class Main {
        static StreamTokenizer r;
        static PrintWriter pr;
        static BufferedReader re;

        static {
            r = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
            pr = new PrintWriter(new OutputStreamWriter(System.out));
            re = new BufferedReader(new InputStreamReader(System.in));
        }


        public static void main(String[] args) throws IOException {
            int cord[] = new int[5001];
            for (int i = 1; i <= 5000; i++) {
                cord[i] = getFactors(i)+1;
            }

            int T = ini();
            int a;
            int b;
            int count[] = new int[T];
            for (int i = 0; i <count.length ; i++) {
                a = ini();
                b = ini();
                count[i] =sp(a,b,cord);
            }

            for (int i = 0; i < count.length; i++) {
                pr.println(count[i]);
                pr.flush();
            }
        }

        static int sp(int n, int m, int co[]) {
            int max = 0;
            int index = n;
            for (int i = n; i <= m; i++) {
                index = co[i] > max ? i : index;
                max = co[i] > max ? co[i] : max;
            }
            return index;
        }

        static int getFactors(int n) {//获得因子数
/*只用计算1到n的平方根之间的数,这是因为当我们得出n % i == 0时,同时确定了i和n/i两个因子,不用重复计算.
举个栗子:64的因子:1,2,4,8,16,32,64   64的平方根8相当于一个分界线的作用*/
            int count = 0;
            if (n == 0)
                return count;
            else {
                for (int i = 1; i <= Math.sqrt(n); i++) {
                    if (n % i == 0) {
                        if (i != n / i) {//考虑n=1的特殊情况
                            count += 2;
                        } else {
                            count += 1;
                        }
                    }
                }
                return count ;
            }
        }
        
        static int ini() throws IOException {
            r.nextToken();
            return (int) r.nval;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值