zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    shell75叠加
    shell73while ping测试脚本
    shell72while读文件创建用户
    shell70批量修改远程主机的ssh配置文件内容
    shell68批量创建用户(传多个参数)
    js限制input输入
    php获取textarea的值并处理回车换行的方法
    strtr对用户输入的敏感词汇进行过滤
    mysql执行语句汇总
    js倒计时防页面刷新
  • 原文地址:https://www.cnblogs.com/MartinLwx/p/12533311.html
Copyright © 2011-2022 走看看