当前位置:网站首页>poker question
poker question
2022-08-02 00:19:00 【Xiao Lu wants to brush the force and deduct the question】
前言
一张扑克有3个属性,每种属性有3种值(A、B、C)
比如"AAA",第一个属性值A,第二个属性值A,第三个属性值A
比如"BCA",第一个属性值B,第二个属性值C,第三个属性值A
给定一个字符串类型的数组cards[],每一个字符串代表一张扑克
从中挑选三张扑克,一个属性达标的条件是:这个属性在三张扑克中全一样,或全不一样
挑选的三张扑克达标的要求是:每种属性都满足上面的条件
比如:“ABC”、“CBC”、“BBC”
第一张第一个属性为"A"、第二张第一个属性为"C"、第三张第一个属性为"B",全不一样
第一张第二个属性为"B"、第二张第二个属性为"B"、第三张第二个属性为"B",全一样
第一张第三个属性为"C"、第二张第三个属性为"C"、第三张第三个属性为"C",全一样
每种属性都满足在三张扑克中全一样,或全不一样,所以这三张扑克达标
返回在cards[]中任意挑选三张扑克,达标的方法数
解题思路

choose twoAAA,The third one is requiredAAA
一共27个牌面,必须选3How many possibilities are there for different cards
If required3个牌面,If it hits the mark,那这3How many combinations are there for each ranking?
100* 50 * 60
最暴力的就是27个牌面,Enumerate how many different three cards there are
The final solution is to multiply several numbers on the line
一共27个牌面,每个牌面要跟不要,But once and beyond3种, 就停止,
It was just right in the end3个排名,If found each-位都- sample or each-The bits are different
Multiply the number of them and you're done
The most important technique is to guess the solution based on the amount of data
我一看100Thousands of cards,I knew it was wrong to think in terms of cards,
You must start with the cards,因为只有27种牌面
代码
public static int ways(String[] cards) {
int[] counts = new int[27];
for (String s : cards) {
char[] str = s.toCharArray();
counts[(str[0] - 'A') * 9 + (str[1] - 'A') * 3 + (str[2] - 'A') * 1]++;
}
int ways = 0;
for (int status = 0; status < 27; status++) {
int n = counts[status];
if (n > 2) {
ways += n == 3 ? 1 : (n * (n - 1) * (n - 2) / 6);
}
}
LinkedList<Integer> path = new LinkedList<>();
for (int i = 0; i < 27; i++) {
if (counts[i] != 0) {
path.addLast(i);
ways += process2(counts, i, path);
path.pollLast();
}
}
return ways;
}
// 之前的牌面,拿了一些 ABC BBB ...
// pre = BBB
// ABC ...
// pre = ABC
// ABC BBB CAB
// pre = CAB
// 牌面一定要依次变大,所有形成的有效牌面,把方法数返回
public static int process(int[] counts, int pre, LinkedList<Integer> path) {
if (path.size() == 3) {
return getWays2(counts, path);
}
int ways = 0;
for (int next = pre + 1; next < 27; next++) {
if (counts[next] != 0) {
path.addLast(next);
ways += process2(counts, next, path);
path.pollLast();
}
}
return ways;
}
public static int getWays(int[] counts, LinkedList<Integer> path) {
int v1 = path.get(0);
int v2 = path.get(1);
int v3 = path.get(2);
for (int i = 9; i > 0; i /= 3) {
int cur1 = v1 / i;
int cur2 = v2 / i;
int cur3 = v3 / i;
v1 %= i;
v2 %= i;
v3 %= i;
if ((cur1 != cur2 && cur1 != cur3 && cur2 != cur3) || (cur1 == cur2 && cur1 == cur3)) {
continue;
}
return 0;
}
v1 = path.get(0);
v2 = path.get(1);
v3 = path.get(2);
return counts[v1] * counts[v2] * counts[v3];
}
边栏推荐
- A simple file transfer tools
- 控制电机的几种控制电路原理图
- Arduino 基础语法
- Deliver cloud-native microservices applications with Zadig
- Statement执行update语句
- 为什么要使用MQ消息中间件?这几个问题必须拿下
- 具有通信时延的多自主体系统时变参考输入的平均一致性跟踪
- JSP page指令errorPage属性起什么作用呢?
- How to reinstall Win11?One-click method to reinstall Win11
- 众筹DAO“枯萎”的缩影:曾拍下《沙丘》未出版手稿的Spice DAO解散
猜你喜欢
随机推荐
LeetCode_518_零钱兑换Ⅱ
08-SDRAM: Summary
JSP page指令errorPage属性起什么作用呢?
LeetCode_322_零钱兑换
Multi-feature fusion face detection based on attention mechanism
不要用jOOQ串联字符串
07-SDRAM: FIFO control module
An Enhanced Model for Attack Detection of Industrial Cyber-Physical Systems
06-SDRAM :SDRAM控制模块
Multidimensional Correlation Time Series Modeling Method Based on Screening Partial Least Squares Regression of Correlation Variables
如何设计循环队列?快进来学习~
22. The support vector machine (SVM), gaussian kernel function
【解决】win10下emqx启动报错Unable to load emulator DLL、node.db_role = EMQX_NODE__DB_ROLE = core
bgp 聚合 反射器 联邦实验
Short video seo search optimization main content
els 方块变形
面试必问的HashCode技术内幕
How to reinstall Win11?One-click method to reinstall Win11
An interesting project--Folder comparison tool (1)
不了解SynchronousQueue?那ArrayBlockingQueue和LinkedBlockingQueue不会也不知道吧?








