Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
怎么由质数转化为高斯质数,如13..它们的规律?那里出错了?#include <iostream> #include <cmath> #include <iomanip> #include<string> #include<vector> using namespace std; int main() { float N; vector<int> result; int r=1; while(cin>>N) { int i=2,k,flag=0,flag1=0; while(1) //永远循环,get prime factors of N { k=int(sqrt(N)); int m=N; for (;i<=k;i++) if(m%i==0) //i肯定是质数,N肯定不是质数 { flag=1; flag1=1; result.push_back(i); N=N/i; break ;//for the break in the way,the i won't increase } if(flag) {flag=0;continue;} if(flag1) { result.push_back(N); } else result.push_back(N); break; } int len=result.size();//get the number of the prime factors int *p=new int[len*2]; int rp=0;//record the Gaossian prime. int front=0; cout<<"Case #"<<r<<": "; //cout the first number int complex;//judge wethere there is the type of complex int kp; int a,b; for(int j=0;j<len;j++)//get the Gaosssian Prime factors stored in *p; { complex=0; a=0; b=0; if(result[j]!=front) { kp=(int)sqrt((float)result[j]); for(int kr=kp;kr>=1;kr--) { if(kp*kp+kr*kr==result[j]) { a=kr; b=kp; complex=1; break; } } if(complex==1) { p[rp*2]=a; p[rp*2+1]=b; } else { p[rp*2]=result[j]; p[rp*2+1]=0; } rp++; front=result[j]; } } for(int ro=0;ro<rp-1;ro++)//order the value in p by desc { int maxr=ro; for(int ron=ro+1;ron<rp;ron++) { if(p[maxr*2]<p[ron*2]||p[maxr*2]==p[ron*2]&&p[maxr*2+1]<p[ron*2+1]) { maxr=ron; } } if(maxr!=ro) { int ra=p[ro*2],rb=p[ro*2+1]; p[ro*2]=p[maxr*2],p[ro*2+1]=p[maxr*2+1]; p[maxr*2]=ra,p[maxr*2+1]=rb; } } for(int h=rp-1;h>=0;h--)//output the result by accquirment { if(h==rp-1) { if(p[h*2+1]!=0) { if(p[h*2+1]==1) { printf("%d+j, %d-j",p[h*2],p[h*2]); } else printf("%d+%dj, %d-%dj",p[h*2],p[h*2+1],p[h*2],p[h*2+1]); } else { cout<<p[h*2]; } } else { if(p[h*2+1]!=0) { if(p[h*2+1]==1) { printf(", %d+j, %d-j",p[h*2],p[h*2+1]); } else { printf(", %d+%dj, %d-%dj",p[h*2],p[h*2+1],p[h*2],p[h*2+1]); } } else { cout<<", "<<p[h*2]; } } } cout<<endl; r++; result.clear(); delete []p; } return 0; Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator