跳到正文

个人记录

分解质因数工具

DoctorRyan约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 1 分钟
获赞
1

版本

  1. v1当前
C++
#include<iostream>
#include<conio.h>

using namespace std;

bool is_prime(int n) {
	int cnt = 0;
	for(int i = 1; i * i <= n; i++) {
		if(n % i == 0) {
			cnt += 2;
		}
	}
	if(cnt == 2) {
		return true;
	}
	else {
		return false;
	}
}

int n, ans[35], t;

int main() {
	cout << "Please enter a number:" << endl;
	cin >> n;
	cout << "Prime factorization:" << endl;
	cout << n << " = ";
	for(int i = 2; i * i <= n; i++) {
		while(n % i == 0) {
			n /= i;
			t++;
			ans[t] = i;
		}
	}
	if(n > 1) {
		ans[++t] = n;
	}
	cout << ans[1];
	for(int i = 2; i <= t; i++) {
		cout << " * " << ans[i];
	}
	cout << endl;
	cout << "Please press any key to exit.";
	getch();

	return 0;
}
原文