L1-006. 连续因子
2017-05-08 10:45
393 查看
题目地址
https://www.patest.cn/contests/gplt/L1-006题目描述
一个正整数N的因子中可能存在若干连续的数字。例如630可以分解为3*5*6*7,其中5、6、7就是3个连续的数字。给定任一正整数N,要求编写程序求出最长连续因子的个数,并输出最小的连续因子序列。输入格式:
输入在一行中给出一个正整数N(1 < N < 2^31)。
输出格式:
首先在第1行输出最长连续因子的个数;然后在第2行中按“因子1*因子2*……*因子k”的格式输出最小的连续因子序列,其中因子按递增顺序输出,1不算在内。
输入样例:
630
输出样例:
3 5*6*7
ac
数据用long long类型,不用int素数区别
连续因子
#include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <vector> #include <map> #include <cmath> #include <iostream> #include <queue> using namespace std; const int N = 1005; typedef long long int LL; bool isPrime(LL n) { if(n == 2) return true; for(LL i= 2;i*i<=n;i++) { if(n % i == 0) return false; } return true; } int main() { //freopen("in.txt", "r", stdin); LL n; while(scanf("%lld",&n) != EOF) { if(isPrime(n)) { printf("1\n%lld\n",n); continue; } LL ansCnt = 0; LL staNum = 0; LL sta = 2; LL minSums = n; while(sta * sta <= n) { if(n % sta == 0) { LL sums = sta; int cnt = 1; for(LL k=1;k<=31;k++) { sums *= (sta+k); if(n % sums == 0) { cnt ++; }else{ if(cnt > ansCnt) { ansCnt = cnt; staNum = sta; minSums = sums; } /* else if(cnt == ansCnt) { if(sums < minSums) { staNum = sta; minSums = sums; } } */ sta = sta + 1;//cnt; break; } } }else{ sta ++; } } printf("%lld\n%lld",ansCnt,staNum); for(LL i=staNum+1;i<staNum+ansCnt;i++) { printf("*%lld",i); } printf("\n"); } //printf("\n"); return 0; }
相关文章推荐
- L1-006. 连续因子(枚举因子)
- L1-006. 连续因子
- L1-006. 连续因子
- L1-006连续因子
- L1-006 连续因子(枚举法)
- PAT团体程序设计天梯赛 L1-006. 连续因子
- 天梯赛L1-006连续因子
- PAT-CCCC练习:L1-006.连续因子
- L1-006. 连续因子
- CCCC/GPLT L1-006 连续因子
- (PAT)L1-006 . 连续因子
- L1-006. 连续因子
- L1-006. 连续因子
- L1-006. 连续因子
- pat L1-006. 连续因子
- L1-006.连续因子
- L1-006. 连续因子
- PAT-天梯赛练习集-L1-006-连续因子
- PAT L1-006. 连续因子
- 团体程序设计天梯赛-练习集 L1-006. 连续因子 JAVA