SiriBlog

siriyang的个人博客


  • 首页

  • 排行榜

  • 标签115

  • 分类37

  • 归档320

  • 关于

  • 搜索

Bulls and Cows的Java求解过程遇到的问题

发表于 2020-04-16 更新于 2022-05-24 分类于 计算机 , 算法题 , 技术 , Java 阅读次数: Valine:
本文字数: 3.4k 阅读时长 ≈ 3 分钟

2020.04.16

Note1

  Java创建链表:

1
List<int[]> numberList = new ArrayList<int[]>();

Note2

  Java格式化创建字符串:

1
String str=String.format("[%d; %d; %d; %d]", c[0], c[1], c[2], c[3]);

简单策略解法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Main {

private static List<int[]> numberList;


public static void main(String[] args) {

InitNumberList();
int size=numberList.size();
System.out.println(size);
for(int i=0;i<size;i++){
InitNumberList();
String str=Print(numberList.get(i));
int step=GuessIt(numberList.get(i));
if(step>8)
System.out.printf("%d %d %s\n",i,step,str);
}


}

public static int GuessIt(int[] result)
{
int a = 0;
int b = 0;
int step = 0;
while (a != 4)
{
int[] currentNumber = numberList.get(0);
a = GetA(result, currentNumber);
b = GetAB(result, currentNumber) - a;
RemoveA(a, currentNumber);
RemoveAB(a + b, currentNumber);
// System.out.printf("Current Guess: %s, Return: %dA%dB, The Result Is: %s\n",
// Print(currentNumber), a, b, Print(result));
step++;
}
//System.out.printf("Step: %d\n", step);
return step;
}

public static String Print(int[] c)
{
return String.format("[%d; %d; %d; %d]", c[0], c[1], c[2], c[3]);
}

public static void InitNumberList(){
numberList = new ArrayList<int[]>();
Set hs = new HashSet();
for (int a = 0; a <= 9; a++) {
hs.add(a);
for (int b = 0; b <= 9; b++) {
if (hs.contains(b))
continue;
hs.add(b);
for (int c = 0; c <= 9; c++) {
if (hs.contains(c))
continue;
hs.add(c);
for (int d = 0; d <= 9; d++){
if (hs.contains(d))
continue;
numberList.add(new int[]{a, b, c, d});
}
hs.remove(c);
}
hs.remove(b);
}
hs.remove(a);
}
}

public static int GetA(int[] number, int[] guessNumber)
{
int count = 0;
for (int i = 0; i < 4; i++)
if (number[i] == guessNumber[i]) count++;
return count;
}

public static int GetAB(int[] number, int[] guessNumber)
{
int count = 0;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (number[i] == guessNumber[j]) count++;
return count;
}

public static void RemoveA(int a, int[] guessNumber)
{
for (int k = numberList.size() - 1; k >= 0; k--)
{
if (GetA(numberList.get(k), guessNumber) != a)
numberList.remove(k);
}
}

public static void RemoveAB(int ab, int[] guessNumber)
{
for (int k = numberList.size() - 1; k >= 0; k--)
{
if (GetAB(numberList.get(k), guessNumber) != ab)
numberList.remove(k);
}
}

}
-------- 本文结束 感谢阅读 --------
相关文章
  • 算法常用模板:并查集
  • Word使用中遇到的一些问题
  • OpenCV3使用中遇到的一些问题
  • Qt使用中遇到的一些问题
  • NLP:阅读理解综述
觉得文章写的不错的话,请我喝瓶怡宝吧!😀
SiriYang 微信支付

微信支付

SiriYang 支付宝

支付宝

  • 本文标题: Bulls and Cows的Java求解过程遇到的问题
  • 本文作者: SiriYang
  • 创建时间: 2020年04月16日 - 22时04分
  • 修改时间: 2022年05月24日 - 00时05分
  • 本文链接: https://blog.siriyang.cn/posts/20200416225934id.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
学习笔记 算法题 Java
2020华为云大数据挑战赛-热身赛(1)
基于pyqt的图片批量文字标注程序
SiriYang

SiriYang

努力搬砖攒钱买镜头的摄影迷
320 日志
33 分类
88 标签
RSS
GitHub E-Mail
Creative Commons
Links
  • 友情链接
  • 作品商铺

蜀ICP备19008337号 © 2019 – 2025 SiriYang | 1.7m | 25:41
0%