问题
Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1k1×p2k2×⋯×pmkm.
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 = p1k1*p2k2*…*pm^km, where pi’s are prime factors of N in increasing order, and the exponent ki is the number of pi – hence when there is only one pi, ki is 1 and must NOT be printed out.
Sample Input:
97532468
Sample Output:
97532468=2^211171011291
解决方法
分析:素数表的建立。
题⽬⼤意:给出⼀个整数,按照从⼩到⼤的顺序输出其分解为质因数的乘法算式。
#include<iostream>
#include<math.h>
#include<map>
using namespace std;
int main()
{
int n;
map<int, int>m;
scanf("%d", &n);
printf("%d=", n);
if (n == 1)
{
printf("1\n");
return 0;
}
for (int i = 2; i <= sqrt(n); i++)
{
while (n%i == 0)
{
m[i]++;
n = n / i;
}
}
if (n > 1) m[n]++;
for (auto it = m.begin(); it != m.end(); it++)
{
it == m.begin() ? printf("%d", it->first) : printf("*%d", it->first);
if(it->second>1) printf("^%d", it->second);
}
return 0;
}
后记
这个题最开始读题不仔细,走了很多的弯路,所以读题一定要认真。。。o(╥﹏╥)o其实内心想把自己拍死。