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

What's wrong?

Posted by tur5 at 2009-05-17 01:58:15 on Problem 3048 and last updated at 2009-05-17 01:59:39
Output

* Line 1: The integer with the largest prime factor. If there are more than one, output the one that appears earliest in the input file.


So I think
2
19
38

this input should out like this

19

but,

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int primes[20001];

int main()
{
	int n, a, i, j, maxfac = 0, max = 0, max2 = 0, primecnt;

	primes[0] = 2;
	primecnt = 1;
	for(i = 3; i < 20000; i = i + 2)
	{
		for(j = 0; j < primecnt; j++)
		{
			if(i % primes[j] == 0)
			{
				break;
			}
		}
		if(j == primecnt)
		{
			primes[primecnt++] = i;
		}
	}
	
	scanf("%d", &n);
	if (n == 1)
	{
		scanf("%d", &a);
		printf("%d\n", a);
	}
	else
	{
		for (j = 0; j < n; j++)
		{
			scanf("%d", &a);
			for (i = primecnt - 1; i >= 0; i--)
			{
				if (primes[i] <= a && a % primes[i] == 0)
				{
					maxfac = primes[i];
					break;
				}
			}

			if (max2 <= maxfac)
			{
				max2 = maxfac;
				max = a;
			}
		}
		
		printf("%d\n", max);
	}
	
	return 0;
}

this code makes output 19, but it got WA,

#include <iostream>
#include <algorithm>

using namespace std;

int prime[10000],a[5001];

int main()
{
	int i, j, m = 1, n, max, s;
	prime[0] = 2;
	
	for(i = 3; i < 20000; i = i + 2)
	{
		for(j = 0; j < m; j++)
		{
			if(i % prime[j] == 0)
			{
				break;
			}
		}
		if(j == m)
		{
			prime[m++] = i;
		}
	}
	
	cin >> n;
	for(i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	if(n == 1)
	{
		cout << a[0] << endl;
	}
	else
	{
		sort(a, a + n);
		s = max = 0;
		for(i = n - 1; i >= 0; i--)
		{
			for(j = m - 1; j >= 0; j--)
			{
				if(a[i] >= prime[j])
				{
					if(a[i] % prime[j] == 0)
					{
						if(max < prime[j])
						{
							max = prime[j];
							s = a[i];
							break;
						}
					}
				}
			}
		}
		cout << s << endl;
	}
	return 0;
}

this code makes output 38, but it got AC.

Latest input is earlier appeared in input file?

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