package Guess_Numbers;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class Guess_Numbers {
static Map<String,Integer> map1 = new HashMap<String,Integer>();
static Map<String,Integer> map2 = new HashMap<String,Integer>();
static Map<String,Integer> map3 = new HashMap<String,Integer>();
public void init1(){
map1.put("6403", 2);
map1.put("3659", 2);
map1.put("0197", 1);
map1.put("8964", 1);
map1.put("5830", 22);
map1.put("1386", 2);
}
public void init2(){
map2.put("3729", 20);
map2.put("1697", 2);
map2.put("7812", 2);
map2.put("4129", 20);
map2.put("5268", 1);
map2.put("2931", 3);
}
public void init3(){
map3.put("3419", 2);
map3.put("8345", 2);
map3.put("5974", 11);
map3.put("6723", 1);
map3.put("7081", 1);
map3.put("9837", 2);
}
public void get_answer(Map<String , Integer> map){
String answer = "";
int count = 0;
int result = 0;
for(int i = 1234;i < 9999;i++){
String s = i + "";
count = 0;
for (String key : map.keySet()) {
result = 0;
for(int j = 0; j < 4;j++){
if(s.charAt(j) == key.charAt(j)){
result++;
}
//System.out.println(result);
}
result = result * 10;
for(int j = 0;j < s.length();j++){
for(int k = 0;k < key.length();k++){
if(s.charAt(j) == key.charAt(k) && k!=j){
result++;
// System.out.println(result);
}
}
}
//System.out.println(result);
if(result == map.get(key)){
count++;
//System.out.println(count);
}
}
if(count == 6){
answer = s;
break;
}
}
System.out.println(answer);
}
public static void main(String[] args) {
Guess_Numbers one = new Guess_Numbers();
one.init1();
one.get_answer(map1);
one.init2();
one.get_answer(map2);
one.init3();
one.get_answer(map3);
}
}
包含了遍歷和賦值
小白筆記------------------Map的用法
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.