Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:我就是素数分解做的……

Posted by 2007011348 at 2007-12-21 20:22:57 on Problem 3048
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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator