• UVA 294 约数 Divisors


    前言:

    我没开long long在VJ上竟然过了!!!

    题面翻译

    给定 n n n 个数值区间 [ l , r ] [l,r] [l,r],求出每个区间内约数个数最大的数,并按照以下格式输出:

    Between [l] and [r], [maxn_num] has a maximum of [maxn_divisor] divisors.
    
    • 1

    样例输入:

    3
    1 10
    1000 1000
    999999900 1000000000
    
    • 1
    • 2
    • 3
    • 4

    样例输出:

    Between 1 and 10, 6 has a maximum of 4 divisors.
    Between 1000 and 1000, 1000 has a maximum of 16 divisors.
    Between 999999900 and 1000000000, 999999924 has a maximum of 192 divisors.
    
    • 1
    • 2
    • 3

    对于 100 % 100\% 100% 的数据:

    1 ≤ l i < r i ≤ 1 0 10 1 \leq l_i1li<ri1010 1 ≤ r − l ≤ 1 0 4 1 \leq r-l \leq 10^{4} 1rl104

    题目描述

    PDF

    输入格式

    输出格式

    样例 #1

    样例输入 #1

    3
    1 10
    1000 1000
    999999900 1000000000
    
    • 1
    • 2
    • 3
    • 4

    样例输出 #1

    Between 1 and 10, 6 has a maximum of 4 divisors.
    Between 1000 and 1000, 1000 has a maximum of 16 divisors.
    Between 999999900 and 1000000000, 999999924 has a maximum of 192 divisors.
    
    • 1
    • 2
    • 3

    分析:

    因为题目给的数据范围 1 ≤ r − l ≤ 1 0 4 1 \leq r-l \leq 10^{4} 1rl104不太大,所以我们直接暴力枚举就可以啦。(代码有详细分析)

    代码:

    #include
    
    using namespace std;
    
    int aa(int n)//分解质因数(返回值为n的约数个数)
    {
    	int ans=1;
    	
    	for(int i=2;i<=sqrt(n);i++)
    	{
    		if(n%i==0)//找到了n的质因子
    		{
    			int res=1;
    			
    			while(n%i==0)
    			{
    				res++;
    				
    				n/=i;
    			}
    			
    			ans*=res;
    		}
    	}
    	
    	if(n>1)//如果还有,乘以二
    	{
    		ans*=2;
    	}
    	
    	return ans;
    }
    
    int n;
    
    int l,r;
    
    int maxx=-100;
    
    int k;
    
    int main()
    {
    	cin>>n;
    	
    	while(n--)
    	{
    		maxx=-100;
    		
    		cin>>l>>r;
    		
    		k=0;
    		
    		for(int i=l;i<=r;i++)//在区间中枚举
    		{
    			if(aa(i)>maxx)//只要i的质因数个数比maxx大
    			{
    				maxx=aa(i);//更新maxx
    				
    				k=i;//更新下标
    			}
    		}
    		
    		printf("Between %d and %d, %d has a maximum of %d divisors.\n",l,r,k,maxx);//按要求输出
    	}
    	
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    结束啦~~~

  • 相关阅读:
    【图】按公因数计算最大组件大小 并查集
    【心理学】2022-08-08 日常生活问题回答
    神经网络二分类输出概率,神经网络二分类预测
    c++以十六进制直接给char *赋值
    2-Redis架构设计到使用场景-四种部署运行模式(下)
    ADAS行业进入深水区,高阶自动驾驶迎战行业分水岭
    Dockerfile构建镜像与实战
    ES6:什么是Promise_
    用HTML+CSS做一个漂亮简单的节日网页【元宵节】
    STM32G030F6P6 芯片实验 (二)
  • 原文地址:https://blog.csdn.net/m0_66603329/article/details/126490107