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 |
Re:我就是素数分解做的……In Reply To:Re:我就是素数分解做的…… Posted by:byron at 2006-10-22 23:15:44 请解释一下怎么才不会超时… #include<iostream> #include<cmath> using namespace std; bool isprime(int k) { int i; for(i=2;i<=sqrt(k);i++) { if(k % i==0)return 0; } return 1; } int main() { const int maxn=5001; int n; int a[maxn],f[maxn]; int i,j,temp; cin>>n; for(i=1;i<=n;i++) { cin>>a[i]; f[i]=1; } for(i=1;i<=n;i++) { for(j=2;j<=a[i];j++) { if(isprime(j)==1 && a[i] % j == 0) { f[i]=j; } } } for(j=1;j<=n-1;j++) { for(i=1;i<=n-j;i++) { if(f[i]<f[i+1]) { temp=a[i]; a[i]=a[i+1]; a[i+1]=temp; } } } cout<<a[1]<<endl; return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator