1059. Prime Factors (25)

#include <iostream>
#include <math.h>

using namespace std;

int judge(long int n)
{
	long int i, r = sqrt(n * 1.0);
	for(i = 2; i <= r; i++)
	{
		if(n % i == 0)
		{
			return 0;
		}
	}

	return 1;
}

int main()
{
	long int n;
	scanf("%d", &n);

	printf("%ld=", n);

	if(n == 1 || judge(n) == 1)
	{
		printf("%ld\n", n);
		return 0;
	}

	long int i = 2, count;
	while(n > 1)
	{
		while(n % i > 0)
		{
			i++;
		}

		count = 0;
		while(n % i == 0)
		{
			count++;
			n /= i;
		}

		printf("%ld", i);

		if(count > 1)
		{
			printf("^%ld", count);
		}

		if(n > 1)
		{
			printf("*");
		}
	}

	printf("\n");

	system("pause");
	return 0;
}

 

posted on 2025-11-23 17:11  王景迁  阅读(2)  评论(0)    收藏  举报

导航