PAT连续因子试题链接
刚开始想最长连续因子不会超过11个,导致查找范围严重缩小,后来在网上找了个AC代码,和我的diff( Linux下的命令)比较一下,就发现错哪了。这个diff很好用的(zzuwenjie推荐哈)。2017-3-12 10:51:22
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
//const int maxn = 100+4;
int main(){
int n;
while (cin >> n){
//两重循环找答案
int len = 0, start;
int square = (int)sqrt( n+ 0.5);
for (int i = 2; i <= square; ++i){//错的范围:1,
int L = 0, tmp = 1;
for (int j = i; j <= square + 5; ++j){//错的范围:12
if ((n % j == 0) && ((tmp *= j) <= n) && (n % tmp == 0)){
L++;
} else {
if (len < L){
len = L;
start = i;
}
break;
}
}
}
//若只有一个连续的因子,必须是最小的因子
//下面是输出结果
if (len == 0){
printf("1\n%d\n", n);
}else{
printf("%d\n%d", len, start++);
for (int i = 1; i < len; ++i){
printf("*%d", start++);
}
puts("");
}
}
return 0;
}
\