• PTA(Advanced Level)1096.Consecutive Factors


    Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 3×5×6×7, where 5, 6, and 7 are the three consecutive numbers. Now given any positive N, you are supposed to find the maximum number of consecutive factors, and list the smallest sequence of the consecutive factors.

    Input Specification:

    Each input file contains one test case, which gives the integer N (1<N<231).

    Output Specification:

    For each test case, print in the first line the maximum number of consecutive factors. Then in the second line, print the smallest sequence of the consecutive factors in the format factor[1]*factor[2]*...*factor[k], where the factors are listed in increasing order, and 1 is NOT included.

    Sample Input:
    630
    
    Sample Output:
    3
    5*6*7
    
    思路
    • 找一个数字(n)的因数连乘中存在的最长序列,且要求这个序列为1的等差数列。
    • 方法:从小到大开始找,一个数除了本身,最大的因数不可能超过(sqrt{n}),这就是循环范围。如果当前为因数,那么检查这个数的下一个,看最长能多少
    • (N)的最大是(2^{31}-1)(sqrt{n}approx2^{15.5}),因为要测试连乘,所以用int会超时,使用long long即可
    代码
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    int main()
    {
    	LL n;
    	cin >> n;
    	LL up = (LL)sqrt(n * 1.0);
    	LL ans = 0, length = 0;
    	for(LL i=2;i<=up;i++)
    	{
    		LL tmp = 1;
    		LL step = i;
    		while(true)
    		{
    			tmp *= step;
    			if(n % tmp != 0)	break;	//不是因子
    			if(step - i + 1 > length)
    			{
    				ans = i;
    				length = step - i + 1;
    			}
    			step++;
    		}
    	}
    	if(length == 0)		//也就是这个是质数,因子只有1和自己
    	{
    		cout << 1 << endl;
    		cout << n;		//不能输出1这个因子
    	}
    	else
    	{
    		cout << length << endl;
    		for(LL i=0;i<length;i++)
    		{
    			cout << ans + i;
    			if(i != length - 1)
    				cout << "*";
    		}
    	}
    	return 0;
    }
    
    
    引用

    https://pintia.cn/problem-sets/994805342720868352/problems/994805370650738688

  • 相关阅读:
    JavaScript学习总结(一)——ECMAScript、BOM、DOM(核心、浏览器对象模型与文档对象模型)
    IDEL——maven的搭建
    JDBC——Mysql 5.7绿色版配置安装过程
    JAVA的面向对象编程--------课堂笔记
    Javaweb第九章、jsp引入JSTL
    jsp引入JSTL后实现jsp的解耦
    servret的引入
    网页设计学习笔记小结
    jdk和Tomcat环境变量设置
    SLZ-VMware虚拟机_ORACLE安装监听器
  • 原文地址:https://www.cnblogs.com/MartinLwx/p/12533311.html
Copyright © 2020-2023  润新知