PAT A1096 Consecutive Factors (20分)

题目链接https://pintia.cn/problem-sets/994805342720868352/problems/994805370650738688

题目描述
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.

输入
Each input file contains one test case, which gives the integer N (1<N<2^​31​​ ).

输出
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.

样例输入
630

样例输出
3
567

代码

#include <cstdio>

int main (){
	long long n, num = 0, max = 0, d = 0, i;
	scanf("%lld", &n);
	for(i = 2; i * i <= n; i++) {
		long long m = n, t = i, j = 0;
		while(m % t == 0) {
			m /= t++;
			j++;
			if(j > max) {
				max = j;
				d = i;
			}
		}		
	}
	if(max == 0)
		printf("1\n%lld\n", n);
	else {
		printf("%lld\n", max);
		for(i = 0; i < max; i++) {
			if(i)
				printf("*");
			printf("%lld", d + i);
		}
		printf("\n");
	}
	return 0;
}
发布了328 篇原创文章 · 获赞 12 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Rhao999/article/details/105053145