首页 > 专题系列 > Java解POJ > POJ 1010 STAMPS [解题报告] Java
2013
11-08

POJ 1010 STAMPS [解题报告] Java

STAMPS

问题描述 :

Have you done any Philately lately?

You have been hired by the Ruritanian Postal Service (RPS) to design their new postage software. The software allocates stamps to customers based on customer needs and the denominations that are currently in stock.

Ruritania is filled with people who correspond with stamp collectors. As a service to these people, the RPS asks that all stamp allocations have the maximum number of different types of stamps in it. In fact, the RPS has been known to issue several stamps of the same denomination in order to please customers (these count as different types, even though they are the same denomination). The maximum number of different types of stamps issued at any time is twenty-five.

To save money, the RPS would like to issue as few duplicate stamps as possible (given the constraint that they want to issue as many different types). Further, the RPS won’t sell more than four stamps at a time.

输入:

The input for your program will be pairs of positive integer sequences, consisting of two lines, alternating until end-of-file. The first sequence are the available values of stamps, while the second sequence is a series of customer requests. For example:

1 2 3 0 ; three different stamp types

7 4 0 ; two customers

1 1 0 ; a new set of stamps (two of the same type)

6 2 3 0 ; three customers

Note: the comments in this example are *not* part of the data file; data files contain only integers.

输出:

For each customer, you should print the “best” combination that is exactly equal to the customer’s needs, with a maximum of four stamps. If no such combination exists, print “none”.

The “best” combination is defined as the maximum number of different stamp types. In case of a tie, the combination with the fewest total stamps is best. If still tied, the set with the highest single-value stamp is best. If there is still a tie, print “tie”.

For the sample input file, the output should be:

7 (3): 1 1 2 3

4 (2): 1 3

6 —- none

2 (2): 1 1

3 (2): tie

That is, you should print the customer request, the number of types sold and the actual stamps. In case of no legal allocation, the line should look like it does in the example, with four hyphens after a space. In the case of a tie, still print the number of types but do not print the allocation (again, as in the example).Don’t print extra blank at the end of each line.

样例输入:

1 2 3 0	; three different stamp types
7 4 0		; two customers
1 1 0		; a new set of stamps (two of the same type)
6 2 3 0	; three customers

样例输出:

7 (3): 1 1 2 3 
4 (2): 1 3 
6 ---- none
2 (2): 1 1
3 (2): tie

解题代码:

//* @author: [email protected]
import java.util.*;
public class Main
{
 public static void main(String[] args)
 {
  Scanner in=new Scanner(System.in);
   while(in.hasNext())
   {
	int[] arr=new int[100];
	int n=0;
	while(true)
	{
		int a=in.nextInt();
		if(a==0)break;
		arr[n++]=a;
	}
	Arrays.sort(arr,0,n);
	while(true)
	{
	 int a=in.nextInt();
	 if(a==0)break;
	 int ser=0,num=0,max=0,a1=0,a2=0,a3=0,a4=0;			
	 boolean tie=false;
	 for(int i=0;i< n;i++)
	 {
	  if(arr[i]>a)break;
	  if(arr[i]==a)
	  {
		if(ser==0)
		{
			ser=1;num=1;max=i;a1=i;tie=false;
		}
		else if(ser==1&&num>1)
		{
			ser=1;num=1;max=i;a1=i;tie=false;
		}
	    }
	    for(int j=i;j< n;j++)
	    {
		if(arr[i]+arr[j]>a)break;
		if(arr[i]+arr[j]==a)
		{
			int ser1=1;
			if(i!=j)ser1++;
			if(ser1>ser)
			{
				ser=ser1;max=j;num=2;a1=i;a2=j;tie=false;
			}
			else if(ser1==ser)
			{
				if(num>2||(num==2&&arr[max]< arr[j]))
				{
					ser=ser1;max=j;num=2;a1=i;a2=j;tie=false;
				}
				else if(num==2&&arr[max]==arr[j])
					tie=true;
			}
		}
		for(int k=j;k< n;k++)
		{
			if(arr[i]+arr[j]+arr[k]>a)break;
			if(arr[i]+arr[j]+arr[k]==a)
			{
				int ser1=1;
				if(i!=j)ser1++;if(j!=k)ser1++;
				if(ser1>ser)
				{
					ser=ser1;max=k;num=3;a1=i;a2=j;a3=k;tie=false;
				}
				else if(ser1==ser)
				{
					if(num>3||(num==3&&arr[max]< arr[k]))
					{
						ser=ser1;max=k;num=3;a1=i;a2=j;a3=k;tie=false;
					}
					else if(num==3&&arr[max]==arr[k])tie=true;
				}
			}
			for(int w=k;w< n;w++)
			{
				if(arr[i]+arr[j]+arr[k]+arr[w]>a)break;
				if(arr[i]+arr[j]+arr[k]+arr[w]==a)
				{
					int ser1=1;
					if(i!=j)ser1++;if(j!=k)ser1++;if(k!=w)ser1++;
					if(ser1>ser)
					{
						ser=ser1;max=w;num=4;
						a1=i;a2=j;a3=k;a4=w;
						tie=false;
					}
					else if(ser1==ser&&num==4)
					{
					  if(arr[w]>arr[max])
					  {
						max=w;
						a1=i;a2=j;a3=k;a4=w;
						tie=false;
					  }
					  else if(arr[w]==arr[max])
						tie=true;
					}
				}	
			}
		}
	  }
	}
	if(num==0)System.out.println(a+" ---- none");
	else if(tie)System.out.println(a+" ("+ser+"):"+" tie");
	else {
		System.out.print(a+" ("+ser+"):");
		if(num==1)System.out.println(" "+arr[a1]);
		else if(num==2)System.out.println(" "+arr[a1]+" "+arr[a2]);
		else if(num==3)System.out.println(" "+arr[a1]+" "+arr[a2]+" "+arr[a3]);
		else if(num==4)System.out.println(" "+arr[a1]+" "+arr[a2]+" "+arr[a3]+" "+arr[a4]);
	}
    }
  }	
 }
}

  1. simple, however efficient. A lot of instances it is difficult to get that a??perfect balancea?? among usability and appearance. I must say that youa??ve done a exceptional task with this. Also, the blog masses quite fast for me on Web explore.