本文主要是介绍1096 Consecutive Factors (20 分),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
主要思路:
因为本题是要输出连续的因子,所以只用记录开始的数和长度。
注意:
判断一个数的因子的时候,上界是sqrt(n),但是这个值可以取到,即必须是小于等于,不然会出错。
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long LL;
int main(){LL n;scanf("%lld",&n);LL st,maxL=0;LL sqr = (LL)sqrt(1.0*n);for(LL i=2;i <= sqr; i++){LL temp = 1,j = i;while(1){temp =temp * j;if(n % temp != 0) break;if(j - i + 1 > maxL){maxL = j-i + 1;st = i;}j++;}}if(maxL == 0) printf("1\n%lld",n);else{printf("%lld\n",maxL);for(LL i = st;i< st+maxL; i++){printf(i==st?"%lld":"*%lld",i);}}return 0;
}
这篇关于1096 Consecutive Factors (20 分)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!