L1-006 连续因子 天梯

思路:

素数只有1和本身
合数 暴力枚举 把连续因子最大的记录下来

注意:


AC代码

//思路:
//素数只有1和本身
//合数 暴力枚举 把连续因子最大的记录下来 
#include<iostream>
#include<cmath>
using namespace std;
int main() {
	int N; //输入所求数字
	cin >> N;
	int count = 0; //计数器
	int start = 0; //连续最多因子的开始
	int max_count = 0; //最大因子个数
	int temp, j; // temp, j临时变量
	for (int i = 2; i <= sqrt(N); i++) {
		for (temp = N, j = i; temp % j == 0; j++) {
			count++;
			temp /= j;
		}
		if (count > max_count)
			start = i, max_count = count; // 记录 最多连续因子&连续因子个数
		count = 0; // 计数器归零 
	}
	//output 
	if (max_count) {
		cout << max_count << endl;
		cout << start;
		for (int i = 0; i + 1 < max_count; i++) {
			cout << "*" << ++start;
		}
	}
	else // 素数情况
		cout << 1 << endl << N;
	return 0;
}
Tags: