1096 Consecutive Factors (20 分)

1096 Consecutive Factors (20 分)

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<2​31​​).

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

代码:

还有就是正向的时候可能sum会超  我是这么认为的

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	scanf("%d", &n);
	int kk = sqrt(n);
	int maxn = 0,u = -1; //这边要注意的是不能是比0 小的或者是下面需要判断下  这边最后一个测试点是错误的,  我当初是-0x3f3f3f3f 下面判断就是当不为这个数的时候就输出这个 结果就错了 
	for(int i = 2; i <= kk; i++)
	{
		int sum = n; 
		int j = i;
		while(sum % j == 0){
			sum = sum / j;
			j++;
		}
		if(j - i > maxn){
			maxn = j - i ;
			u = i;
		}
	}
        if(maxn != 0) 
	{
		printf("%d\n",maxn);
		for(int i = u; i < u + maxn ; i ++){
			printf("%d%c",i,"*\n"[i == u + maxn - 1]);
		}
	}else 
		printf("1\n%d\n",n); 
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/galesaur_wcy/article/details/84063084
今日推荐