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

暴搜,1,1的情况也考虑了,怎么会wrong?请大牛们看看(附代码)

Posted by xuhk at 2009-04-16 15:44:43 on Problem 3048
#include <iostream>
#include <math.h>
using namespace std;
bool isprime(int n)
{
	int i;
	if (n == 1)
	{
		return false;
	}
	if (n == 2)
	{
		return true;
	}
	if (n != 1 && n != 2)
	{
		for (i = 2; i <= (int) sqrt((double) n); i++)
		{
			if (n % i == 0)
			{
				return false;
			}
		}
	}
	return true;
}
int main()
{
	int n, num, res = 1, i, j;
	int max = -1;
	cin>>n;
	for (i = 0; i < n; i++)
	{
		cin >> num;
		if(n == 1)
		{
			cout<<num<<endl;
			return 0;
		}
		for (j = num / 2; j >= 2; j--)
		{
			if (num % j == 0)
			{
				if (isprime(j))
				{
					if (max < j)
					{
						max = j;
						res = num;
					}
					break;
				}
			}
		}
	}
	cout << res << 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