PAT-A-1059. Prime Factors (25)

来源:互联网 发布:linux线程安全退出 编辑:程序博客网 时间:2024/05/29 07:29

1059. Prime Factors (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
HE, Qinming

Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1* p2^k2 *…*pm^km.

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 = p1^k1 * p2^k2 *…*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^2*11*17*101*1291
#include<iostream>#include<cstdio>#include<math.h>using namespace std;int mark[100010] = { 0 };int pnum = 0;int prime[100010];void Findprime(){  for (int i = 2; i < 100010; i++)  {    if (mark[i] == 0)    {      prime[pnum] = i;      pnum++;      for (int j = 2 * i; j < 100010; j += i)      {        mark[j] = 1;      }    }  }}struct Factor{  int x;  int count;}fac[10];int main(){  int n;  cin >> n;  if (n == 1)  {    cout << "1=1" << endl;    return 0;  }  Findprime();  cout << n << "=";  int num = 0;  int sqr = (int)sqrt(1.0*n);  for (int i = 0; i < pnum&&prime[i] <= sqr;i++)  {    if (n%prime[i] == 0)    {      fac[num].x = prime[i];      fac[num].count = 0;      while (n%fac[num].x == 0)      {        fac[num].count++;        n = n / fac[num].x;      }      num++;    }    if (n == 1)      break;  }  if (n != 1)  {    fac[num].x = n;    fac[num].count = 1;    num++;  }  for (int i = 0; i < num; i++)  {    if (i>0)      cout << "*";    cout << fac[i].x;    if (fac[i].count > 1)      cout << "^" << fac[i].count;  }  system("pause");  return 0;}


0 0