首页 > 专题系列 > Java解POJ > POJ 2581 Exact Change Only [解题报告] Java
2013
11-11

POJ 2581 Exact Change Only [解题报告] Java

Exact Change Only

问题描述 :

Boudreaux reached over and shook awake Thibodeaux, who had dozed off somewhere in New Mexico. “Where we at?” Thibodeaux groggily yawned.

“Not in Vegas, I gua-ran-tee, but could you get my knapsack?” Boudreaux asked, gesturing to the worn, leather backpack in the back seat of their cherry red Ford Miata.

“Why, is there a problem?”

“Just hand me my knapsack, problem or not.”

Thibodeaux complied, glancing up as Boudreaux slowed the car to a stop in a line of vehicles approaching a toll booth. “$1.65 — Exact change only,” Thibodeaux read the yellow sign on the front of a small wooden building occupied by a lone toll booth operator. “I have to get $1.65 in exact change?” Thibodeaux asked, digging through the knapsack, “all I have are ten quarters, four dimes, and three pennies. I don’t have any nickels . . .”

“Just give me five of the quarters and the four dimes,” Boudreaux replied, holding out his hand.

“Oh yeah,” Thibodeaux said, handing over the coins, “that does add up to $1.65. I wish there were an easy way to figure out if you have an exact monetary amount, given a set of coins.”

“Hmmm,” Boudreaux shrugged, “sounds like a good programming problem.”

输入:

Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets.

A single data set has 1 component:

Start line – A single line:

A B C D E

where:

A: (0.01 <= A <= 5.00) is a decimal number (to two decimal places) of a monetary amount.

B: (0 <= B <= 100) is an integer number of quarters (one quarter = $0.25).

C: (0 <= C <= 100) is an integer number of dimes (one dime = $0.10).

D: (0 <= D <= 100) is an integer number of nickels (one nickel = $0.05).

E: (0 <= E <= 100) is an integer number of pennies (one penny = $0.01).

输出:

For each data set, there will be exactly one line of output. If there exists one or more subsets of the given coins whose values add up to the given monetary amount exactly, the output will be a single line in the form:

A B C D

where A is the number of quarters, B is the number of dimes, C is the number of nickels, and D is the number of pennies, for the subset with the fewest number of coins. Otherwise, the output will be a single line with the statement:

NO EXACT CHANGE

样例输入:

0.45 2 1 1 4
0.75 3 7 1 75

样例输出:

NO EXACT CHANGE
3 0 0 0

解题代码:

//* @author:
import java.util.*;
public class Main {
 static public void main( String [] str ){
   Scanner sc = new Scanner(System.in);
   while(sc.hasNext())
   {
       int a,b,c,d,e,i,j,k,total,s,value;
	int as=0,bs=0,cs=0,ds=0;
	double t;
       t=sc.nextDouble();
       b=sc.nextInt();
       c=sc.nextInt();
       d=sc.nextInt();
       e=sc.nextInt();
	a = (int)(t*100);
	total = 99999;
	s = 0; value = 0;

	for( i=0; i<=b && i< total && value+i*25<=a; i++ )
	{
         s += i;
	  value += i*25;
	  for( j=0 ; j<=c && s+j< total && value+j*10<=a; j++ )
	  {
	    s += j;
	    value += j*10;
	    for( k=0 ; k<=d && s+k< total && value+k*5<=a; k++ )
	    {
		s += k;
		value += k*5;
		if( a-value<=e && s+a-value < total )
		{
		  total = s+a-value;
		  as=i;bs=j;cs=k;ds=a-value;
		}
		s -= k;
		value -= k*5;
	    }
	    s -= j;
	    value -= j*10;
	  }
	  s -= i;
	  value -= i*25;
    }
    if( total == 99999 )
	System.out.printf("NO EXACT CHANGE\n" );
    else System.out.printf( "%d %d %d %d\n", as, bs, cs, ds);
   }
  }
}

  1. #include <cstdio>
    #include <cstring>

    const int MAXSIZE=256;
    //char store[MAXSIZE];
    char str1[MAXSIZE];
    /*
    void init(char *store) {
    int i;
    store['A']=’V', store['B']=’W',store['C']=’X',store['D']=’Y',store['E']=’Z';
    for(i=’F';i<=’Z';++i) store =i-5;
    }
    */
    int main() {
    //freopen("input.txt","r",stdin);
    //init(store);
    char *p;
    while(fgets(str1,MAXSIZE,stdin) && strcmp(str1,"STARTn")==0) {
    if(p=fgets(str1,MAXSIZE,stdin)) {
    for(;*p;++p) {
    //*p=store[*p]
    if(*p<’A’ || *p>’Z') continue;
    if(*p>’E') *p=*p-5;
    else *p=*p+21;
    }
    printf("%s",str1);
    }
    fgets(str1,MAXSIZE,stdin);
    }
    return 0;
    }