原题链接:https://www.patest.cn/contests/pat-a-practise/1096
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<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
//一开始想把所有n的因子种类数都找到,后来发现太麻烦了,其实只要求连续的有多长就可以了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
int main()
{
int n,i,j,index=1;
int mmax=0;
scanf("%d",&n);
for(i=2; i<=(int)sqrt(n); i++)
{
int tmp=n;
int cnt=0;
j=i;
while(tmp%j==0)
{
cnt++;
tmp/=j;
j++;
}
if(cnt>mmax)
{
index=i;
mmax=cnt;
}
}
if(mmax==0)
{
printf("1\n%d\n",n);
return 0;
}
printf("%d\n",mmax);
for(i=0; i<mmax; i++)
printf(i==0?"%d":"*%d",index++);
printf("\n");
return 0;
}
本文提供了一道PAT-A级题目“ConsecutiveFactors”的解答思路及完整代码实现,该题旨在找出正整数N的最大连续因子数量及其最小连续因子序列。

594

被折叠的 条评论
为什么被折叠?



