标签:
n和i要用long long要不然乘着乘着就是负的了
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:630Sample Output:
3 5*6*7
#include<iostream>
#include <vector>
#include <math.h>
using namespace std;
int maxCnt = 0;
int startIndex;
int main(void) {
long long int n, cnt, ntemp;
cin >> n;
for (long long int i = 2; i*i<n&&pow(i, maxCnt) <= n; i++) {
if (n%i == 0) {
cnt = 1;
ntemp = n / i;
int j = i+1;
while (true)
{
if (cnt > maxCnt) {
maxCnt = cnt;
startIndex = i;
}
if (ntemp%j == 0) {
ntemp = ntemp / j;
j++;
cnt++;
}
else {
break;
}
}
//i = j-1;
}
}
if (maxCnt == 0) {
cout << "1" << endl << n;
return 0;
}
cout << maxCnt << endl;
for (int i = 0; i < maxCnt; i++) {
if (i != maxCnt - 1)
cout << startIndex + i << "*";
else
cout << startIndex + i;
}
return 0;
}
1096. Consecutive Factors (20)
标签:
原文地址:http://www.cnblogs.com/zzandliz/p/5023344.html