1059. Prime Factors (25)

102 篇文章 0 订阅
3 篇文章 0 订阅

1059. Prime Factors (25)

时间限制
50 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
  
  
这题和之前的一题很像。http://xujiayu317.blog.163.com/blog/static/2547520920149218497214/
但是这题过程中一直有个结果错误。看了那些ac的代码。发现是要当num=1时要1=1  改了一下就过了(但是1不是质数吧,这属于特殊条件)。

评测结果

时间结果得分题目语言用时(ms)内存(kB)用户
7月17日 20:38答案正确251059C++ (g++ 4.7.2)1308datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确118015/15
1答案正确13084/4
2答案正确11802/2
3答案正确11802/2
4答案正确11802/2
#include<iostream>
#include<math.h>
using namespace std;
class PrimeFactors
{
private:
  long int e,m;
  PrimeFactors* next;
public: 
  void printpf();
  void line(long int ee,long int mm);
  PrimeFactors*head,*sail;
  PrimeFactors()
  {
   head=NULL;
   sail=NULL;
  }
};
void PrimeFactors::printpf()
{   
 cout<<head->e<<"=";
 sail=head;
 head=head->next;
 delete sail;
 while(head->next)
 {
  cout<<head->e;
  if(head->m!=1)
  {
   cout<<"^"<<head->m;
  }
  cout<<"*";sail=head;
      head=head->next;
     delete sail;
 } 
     cout<<head->e;
  if(head->m!=1)
  {
   cout<<"^"<<head->m;
  }
  delete head;
  cout<<endl; 
  head=NULL;
  sail=NULL;
}
void PrimeFactors::line(long int ee,long int mm)
{
 PrimeFactors*p;
 if(head)
 {
  p=new PrimeFactors;
  p->e=ee;
  p->m=mm;
  sail->next=p;
  sail=p;
 }
 else
 {
    head=new PrimeFactors;
    head->e=ee;
    head->m=0;
    sail=head;
 }
 sail->next=NULL;
}
int main()
{
 PrimeFactors pf;
 long int num,add,count;
    cin>>num;
  if(num>1)
  {
 pf.line(num,0);
    add=2;
 count=0; 
 while(num%add==0)
 {
    num/=add;
       count++;
 } 
 if(count!=0)
 {
  pf.line(add,count);
 }
 add=3;
 while(num>2&&add<=sqrt(num))
 {
  count=0;
  while(num%add==0)
  {
   count++;
   num/=add;
  }
  if(count!=0)
  {
   pf.line(add,count);
  }
  add+=2;
 } 
 if(num>2)
   pf.line(num,1);
   pf.printpf();
}
  else cout<<num<<"="<<num<<endl;
 return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值