-
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Set;
- public class A {
- /**
- * @param args
- */
- /**
- * 1,3,9,27,81
- * */
- static int[] x = {1,3,9,27,81};
- static HashMap<Integer, String> hm= new HashMap<Integer, String>();
- public static void main(String[] args) {
- test();
- System.out.println(hm.get(99));
- }
- public static void test(){
- for(int y:x){
- HashMap<Integer, String> hh = (HashMap<Integer, String>) hm.clone();
- if(!hh.isEmpty()){
- Set<Integer> hs = hh.keySet();
- Iterator<Integer> it = hs.iterator();
- while(it.hasNext()){
- int tmp = (Integer)it.next();
- if(y>tmp){
- hm.put(y-tmp, y+"-"+hh.get(tmp));
- }else if(y<tmp){
- hm.put(tmp-y, hh.get(tmp)+"-"+y);
- }
- hm.put(y+tmp, y+"+"+hh.get(tmp));
- }
- }
- if(!hm.containsKey(y)){
- hm.put(y, String.valueOf(y));
- }
- }
- }
- }
有5个砝码 分别是1,3,9,27,81 可以组合成1--121之间任意整数
最新推荐文章于 2020-04-28 19:29:34 发布