您的位置:首页 > 编程语言 > Java开发

hdu 1085 Holding Bin-Laden Captive!(Java)

2015-09-09 21:52 477 查看

Problem Description

(好长啊,根本不想写上来,复制都嫌麻烦)

(所以,我不打算把原文全写上来了,就写句要点吧)

Given some Chinese Coins (硬币) (three kinds– 1, 2, 5), and their number is num_1, num_2 and num_5 respectively, please output the minimum value that you cannot pay with given coins.

Input

Input contains multiple test cases. Each test case contains 3 positive integers num_1, num_2 and num_5 (0<=num_i<=1000). A test case containing 0 0 0 terminates the input and this test case is not to be processed.

Output

Output the minimum positive value that one cannot pay with given coins, one line for one case.

解题思路

设3种硬币的分别有a,b,c个。

首先要理解,如果中间没有断的话,那么把所有的硬币加起来后,再多一块就是我们要的答案即 (a+2*b+5*c+1)

现在要考虑的就是中间的断层了。

每两个相同硬币之间的断层需要由比它小的硬币来补齐。

对于1元硬币,没有比之更小的了(就题中),所以两个1元硬币之间是没有断层的。

对于2元硬币,之间的断层就必须由1元硬币来补齐,所以只要有1元硬币,那两个2元硬币之间是无断层的,但如果连1元硬币都没有,那么1就会出现断层,此时可以直接输出1

对于5元硬币,之间的断层就必须由2元和1元硬币来补齐,所以只要1元硬币和2元硬币的总和大于等于4就行

代码

import java.util.Scanner;

public class Main{

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
if(a==0&b==0&c==0){
return;
}
if(a==0){
System.out.println("1");
}else if(a+2*b<4){
System.out.println(a+2*b+1);
}else{
System.out.println(a+2*b+5*c+1);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java hdu