1. 程式人生 > >1096 Consecutive Factors (20 分)

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;
}