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对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统