Fork me on GitHub

PAT(甲级)渡劫(七)-Consecutive Factors


PAT(甲级)渡劫(七)-Consecutive Factors

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

int main(){
int n;
scanf("%d",&n);
int t = sqrt((double)n);
int s,maxlen = 0;
for(int i = 2 ; i <= t ; i++){
int m = n;
int curlen = 0;
int k = i;
while(m%k == 0){
++curlen;
m/=k;
k++;
}
if(curlen > maxlen){
maxlen = curlen;
s = i;
}
}
if(!maxlen){
printf("1\n%d",n);
return 0;
}
printf("%d\n%d",maxlen,s);
while(--maxlen){
printf("*%d",++s);
}
return 0;
}

运行结果:

坚持原创技术分享,您的支持将鼓励我继续创作
-------------本文结束感谢您的阅读-------------
0%