【质因数表达式】2021-11-7

缘由

void 质因子集(int 数, int* 数组, int* 序)
{
	int 求 = 1, 商 = 0;//因子不含数本身但包含1
	数组[0] = 求;
	while (++求 <= (商 = 数 / 求))
		if (!(数 % 求))
		{
			if (质数判断(求))数组[++*序] = 求;
			if (求 != 商 && 质数判断(商))数组[++*序] = 商;
		}
}
void 质因数表达式()
{//缘由https://ask.csdn.net/questions/7569924?spm=1005.2025.3001.5141
	int z = 2, n = 0, yj[74]{0}, x = 0;
	cin >> n; cout << n << "=";
	if (质数判断(n))cout << n << endl;
	else
	{
		质因子集(n, yj, &x);
		while (x)
		{
			if (n%yj[x] == 0 && n > yj[x])
				cout << yj[x] << "*", n /= yj[x];
			else
				--x;
		}
		cout << n;
	}
}

【质因数表达式】2021-11-7_第1张图片

你可能感兴趣的:(c++,算法,经验分享,笔记,c语言)