1059 Prime Factors (25 分)

Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p
​1
​​
​k
​1
​​
​​ ×p
​2
​​
​k
​2
​​
​​ ×⋯×p
​m
​​
​k
​m
​​
​​ .

Input Specification:
Each input file contains one test case which gives a positive integer N in the range of long int.

Output Specification:
Factor N in the format N = p
​1
​​ ^k
​1
​​ *p
​2
​​ ^k
​2
​​ *…*p
​m
​​ ^k
​m
​​ , where p
​i
​​ 's are prime factors of N in increasing order, and the exponent k
​i
​​ is the number of p
​i
​​ – hence when there is only one p
​i
​​ , k
​i
​​ is 1 and must NOT be printed out.

Sample Input:
97532468
Sample Output:
97532468=2^211171011291
作者: HE, Qinming
单位: 浙江大学
时间限制: 200 ms
内存限制: 64 MB

1.短除法分解质因数
2.0和1 特判

#include
using namespace std;
int main()
{
	int a,tmp;
	cin >> a;
    if(a==0||a==1)
    {
        cout<<a<<"="<<a;
        return 0;
    }
	tmp = a;
	map<int, int>ret;
	while (a > 1)
		for (int i = 2; i <= a; i++)
			if (a % i == 0) {
				a /=i;
				ret[i]++;
				break;
			}
	cout << tmp << "=";
	for (auto it=ret.begin();it!=ret.end();it++)
		printf("%s%d%s%s", it==ret.begin()?"":"*",it->first, it->second > 1 ? "^" : "", it->second > 1 ? to_string(it->second).c_str() : "");
	return 0;
}

你可能感兴趣的:(PAT甲级)