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 的所有因子中,可能存在几个连续的数字。例如,630 可以分解为 3×5×6×7,其中 5、6 和 7 是三个连续的数字。现在给定任何正 N,您应该找到连续因子的最大数量,并列出连续因子的最小序列。   

输入规范:每个输入文件包含一个测试用例,给出整数 N (1<N<2 31 )。   

输出规范:对于每个测试用例,在第一行打印最大数量的连续因子。然后在第二行中,以 factor[1]*factor[2]*...*factor [k] 格式打印连续因子的最小序列,其中因子按递增顺序列出,不包括 1。


 分析:

条件:满足最小,连续,数量最大。

题目:意思不是让你既要连续又要得出该数,而是在连续的因子中n都能连续被分解

如12,答案是2*3,也就是12可以连续被2和3整除,从而分解。

而不是3*4!

不用一定要求出12。

即在n数字的基础上找题设条件的连续因子,满足前提的最佳连续因子,使得n能被这条连续因子分解就是答案。

题目理解好了就挺容易的

AC

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
	long n;
	cin >> n;
	int m = sqrt(n);

	int maxLen = 0, maxI = 0;
	for (int i = 2; i <= m; i++)
	{
	
		int temp = i;
		int j = i;//阶乘的遍历
		
		while (n % temp == 0)
		{
			temp *= ++j;
		}
		if (j - i > maxLen)
		{
			maxLen = j - i;
			maxI = i;

		}
	}

	if (maxLen == 0)
	{
		cout << 1 << endl << n;
	}
	else
	{
		cout << maxLen << endl;
		cout << maxI;
		for (int i = 1; i < maxLen; i++)
			cout << '*' << maxI+i;
	}

	return 0;
}

解析

#include <iostream>
#include <cmath>
using namespace std;
int main() {
    int n;
    cin >> n;
    int m = sqrt(n);
   //设置开平方为边界,如100
    //10 后面的11必定不可能为连续因子,10*11>100,所以这种相乘的最大因子就是根号n
    int maxLen = 0;
    int maxI = 0;//记录最长的连续因子和其开始下标

    for (int i = 2; i <= m; i++) 
        //sqrt(2^31)大概为4-5万次循环左右,
        //由于n < 2^31,而 12!< 2^31 < 13!所以连乘数最大不超过12个(从2乘到12)。从这点看复杂度还是可以的。
        //突然感觉到阶乘的伟大
        
       
    
    {
        int j = i;
        int temp = i;
        while (n % temp == 0)
            //直接用n对temp取余,只要大于了n自然就会将n作为余数,所以就跳出循环了.
         {
            temp *= ++j;
          
        }//取模就代表连续,因为只要是连续相乘的数字相乘只要能够得到想要的数字,那取模的每一段都必能整除
       
        if (j - i > maxLen) {
            maxLen = j - i ;//取出最大的长度以及开始的数字
            maxI = i;
        }
    }

    if (maxLen == 0) { 
        cout << 1 << endl << n;
    }
    //素数的输出
    //因为前面使用了开平方来减少循环次数,所以素数本身就一定不能循环到它本身,也就是maxlen一直不变为0
    
    else {
        cout << maxLen << endl;
        cout << maxI;
        for (int i = 1; i < maxLen; i++) {
            cout << "*" << maxI + i;
        }//这里i=1是因为前面max先输出了
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值