扑克牌四张24的玩法--每日一练20181018

一副扑克牌52张(去掉大小王),从a到k分别是1到13的数字。每次抽出4张,每张牌(听取建议修改原题中为每个数字)只能参加一次运算,通过加减乘除组合运算,如果可以得到24,则输出该四张牌和计算方式。

如果上面题目有点难,可以先实现简单地,四张扑克牌的和为24的,输出四张牌。。
 
 
=====================================
【汉德学院】小程序将和i码邦app进行不断的融合,为大家提供最方便的移动端自学环境。
 

--来自网页客户端
00.jpg
已邀请:

sjing - Google

赞同来自: admin

p:该问题有歧义,每个数字只能参加一次运算应该改为每张牌只能参与一次运算,才比较合理

--来自网页客户端

admin

赞同来自:

随机抽取四张,然后相加得到24,应该是很容易实现的,看哪位成为第一个提供答案的人。

--来自网页客户端

sjing - Google

赞同来自:

    @test
public void contextLoads() {
List<Integer> ins = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
List<String> methods = Arrays.asList("+", "-", "*", "/");
int first;
int second;
int third;
int fourth;
int count;
String sum;
do {
first = new Random().nextInt(ins.size() + 1);
} while (0 == first);
do {
second = new Random().nextInt(ins.size() + 1);
} while (0 == second || second == first);
do {
third = new Random().nextInt(ins.size() + 1);
} while (0 == third || third == first || third == second);
do {
fourth = new Random().nextInt(ins.size() + 1);
} while (0 == fourth || fourth == first || fourth == second || fourth == third);
do {
String s1 = methods.get(new Random().nextInt(methods.size()));
String s2 = methods.get(new Random().nextInt(methods.size()));
String s3 = methods.get(new Random().nextInt(methods.size()));
sum = first + s1 + second + s2 + third + s3 + fourth;
count = calculation(calculation((calculation(first, second, s1).intValue()), third, s2).intValue(), fourth, s3).intValue();
} while (count != 24);
System.out.println("获取到第一对满足24点的组合:" + sum);
}

public BigDecimal calculation(int n, int m, String s) {
BigDecimal b1 = new BigDecimal(n);
BigDecimal b2 = new BigDecimal(m);
if ("+".equals(s)) {
return b1.add(b2);
} else if ("-".equals(s)) {
return b1.subtract(b2);
} else if ("*".equals(s)) {
return b1.multiply(b2);
} else {
return b1.divide(b2, 1, BigDecimal.ROUND_HALF_UP);
}
}


--来自网页客户端

sjing - Google

赞同来自:

        List<Integer> ins = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
List<String> methods = Arrays.asList("+", "-", "*", "/");
List<String> list = new ArrayList<>();
do {
int first;
int second;
int third;
int fourth;
int count;
String sum;
do {
first = new Random().nextInt(ins.size() + 1);
} while (0 == first);
do {
second = new Random().nextInt(ins.size() + 1);
} while (0 == second || second == first);
do {
third = new Random().nextInt(ins.size() + 1);
} while (0 == third || third == first || third == second);
do {
fourth = new Random().nextInt(ins.size() + 1);
} while (0 == fourth || fourth == first || fourth == second || fourth == third);
for (String a : methods) {
for (String b : methods) {
for (String c : methods) {
sum = first + a + second + b + third + c + fourth;
count = calculation(calculation((calculation(first, second, a).intValue()), third, b).intValue(), fourth, c).intValue();
if (count == 24) {
list.add(sum);
}
}
}
}
} while (list.isEmpty());
System.out.println("获取到第一对满足24点的组合:" + list.get(0));
}

public BigDecimal calculation(int a, int b, String s) {
BigDecimal b1 = new BigDecimal(a);
BigDecimal b2 = new BigDecimal(b);
if ("+".equals(s)) {
return b1.add(b2);
} else if ("-".equals(s)) {
return b1.subtract(b2);
} else if ("*".equals(s)) {
return b1.multiply(b2);
} else {
return b1.divide(b2, 1, BigDecimal.ROUND_HALF_UP);
}
}


--来自网页客户端

要回复问题请先登录注册

Template error, template file not found