扑克序列

2023-05-16

扑克序列

题目描述

标题:扑克序列

A A 2 2 3 3 4 4, 一共4对扑克牌。请你把它们排成一行。

要求:两个A中间有1张牌,两个2之间有2张牌,两个3之间有3张牌,两个4之间有4张牌。

请填写出所有符合要求的排列中,字典序最小的那个。

例如:22AA3344 比 A2A23344 字典序小。当然,它们都不是满足要求的答案。

【分析】

       这道题,一看就能得出答案,理由是要想找到字典序最小的且满足条件的序列,那么2一定是这个序列的第一个数,根据“两个2之间有2张牌”这个原则,那么2与2之间的数可以是3、4,这样刚刚好。而“两个3之间有3张牌”,其中两个3之间已经有4和2了,那么现在只能是A这张牌了,剩下的A则放在最放一个空出来的位置。结果为 2342A3A4。

       那么如何要列出所有符合条件的序列呢?我们该如何做呢?

       这个时候,我们可以采用枚举的方法,将所有的排列都列举出来,判断每个排列是否符合条件。当然我们大可不必将所有排列都列出来,在列的过程中就可以判断是否可以排除该排列。采用回溯法。

【程序】

用java语言编写程序,代码如下:

public class PuKeSerial {
	public static void main(String[] args) {
		char[] c = {'2', '3', '4', 'A'};
		int[] used = {0, 0, 0, 0};
		
		char[] s = new char[8];
		p(c, used, 0, 8, s);
	}
	
	public static void p(char[] c, int[] used, int i, int n, char[] s) {
		if(i == n) {
			System.out.print(s[0]);
			for(int j = 1; j < 8; j++)
				System.out.print(s[j]);
			System.out.println();
			return;
		}
		
		for(int k = 0; k < 4; k++) {
			if(used[k] == 1) {
				if((k == 0 && (i - 3 < 0 || s[i - 3] != '2')) ||
						(k == 1 && (i - 4 < 0 || s[i - 4] != '3')) ||
						(k == 2 && (i - 5 < 0 || s[i - 5] != '4')) ||
						(k == 3 && (i - 2 < 0 || s[i - 2] != 'A')))
					continue;
			}
			
			if(used[k] < 2) {
				used[k]++;
				s[i] = c[k];
				p(c, used, i + 1, n, s);
				used[k]--;
			}
		}
	}
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

扑克序列 的相关文章

  • Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

    原文转载自 sunny2038 的CSDN博客文章 原文地址 最近在看Java xff0c 在编译写书上一个例子时 xff0c 由于书上的代码只有一部分 xff0c 于是就自己补了一个内部类 结果编译时出现 xff1a No enclosi
  • 瑞星微开发工具下载镜像的配置方法?

    如何根据 parameter txt 建立配置表 xff1f 首先我们来看下 parameter txt 的内容 xff1a CMDLINE mtdparts 61 rk29xxnand 0x00002000 64 0x00004000 u
  • 特别困的学生(Extraordinarily Tired Students)

    Extraordinarily Tired Students Time limit 3 000 seconds When a student is too tired he can 39 t help sleeping in class e
  • 骰子涂色(Cube painting)

    Cube painting We have a machine for painting cubes It is supplied with three different colors blue red and green Each fa
  • 盒子(Box)

    Box Time limit 3 000 seconds Ivan works at a factory that produces heavy machinery He hasa simple job he knocks up woode
  • 循环小数(Repeating Decimals)

    Repeating Decimals Time limit 3 000 seconds Repeating Decimals The decimal expansion of the fraction 1 33 is wherethe is
  • 反片语(Ananagrams)

    反片语 Ananagrams 输入一些单词 xff0c 找出所有满足如下条件的单词 xff1a 该单词不能通过字母重排 xff0c 得到输入文本中的另外一个单词 在判断是否满足条件时 xff0c 字母不分大小写 xff0c 但在输出时应保留
  • 集合栈计算机(The SetStack Computer)

    The SetStack Computer Time limit 3 000 seconds 题目是这样的 xff1a 有一个专门为了集合运算而设计的 集合栈 计算机 该机器有一个初始为空的栈 xff0c 并且支持以下操作 xff1a PU
  • 代码对齐(Alignment of Code)

    Alignment of Code Time Limit 4000 2000 MS Java Others Memory Limit 32768 32768 K Java Others Total Submission s 958 Acce
  • Ducci序列(Ducci Sequence)

    Ducci Sequence Time limit 3 000 seconds A Ducci sequence is a sequence of n tuples of integers Given an n tuple of integ
  • 卡片游戏(Throwing cards away I)

    Problem B Throwing cards away I Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at
  • 交换学生(Foreign Exchange)

    Problem E Foreign Exchange Input standard input Output standard output Time Limit 1 second Your non profit organization
  • CAN通信物理容错测试checklist

    CAN通信物理容错测试checklist 工作项目中 xff0c 我们有时有些产品CAN总线功能 xff0c 一般情况下我们必须要满足以下几种状况的测试项才算符合要求 一 CAN H与CAN L短路 判定标准 xff1a 短接故障发生后 x
  • 对称轴(Symmetry)

    Symmetry Time limit 3 000 seconds The figure shown on the left is left right symmetric as it is possible to fold the she
  • 打印队列(Printer Queue)

    Printer Queue Time limit 3 000 seconds 分析 首先记录所求时间它在队列中的位置 xff0c 用一个队列存储这些任务的优先级 xff0c 同时也创建一个队列存储对应任务一开始的位置 xff0c 那么当我们
  • 更新字典(Updating a Dictionary)

    Updating a Dictionary Time Limit 1000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description In th
  • 铁轨(Rails)

    Rails Time limit 3 000 seconds Rails There is a famous railway station in PopPush City Country there is incredibly hilly
  • 矩阵链乘(Matrix Chain Multiplication)

    Matrix Chain Multiplication Time Limit 3000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description
  • 天平(Not so Mobile)

    Not so Mobile Time Limit 3000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description Before being

随机推荐

  • 下落的树叶(The Falling Leaves)

    The Falling Leaves Time Limit 3000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description Each yea
  • 四分树(Quadtrees)

    Quadtrees Time Limit 3000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description A quadtree is a r
  • 油田(Oil Deposits)

    Oil Deposits Time Limit 3000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description The GeoSurvCom
  • Abbott的复仇(Abbott's Revenge)

    Abbott 39 s Revenge Time limit 3 000 seconds Abbott s Revenge Abbott s Revenge The 1999 World FinalsContest included a p
  • rockchip rk3568 openwrt修改根文件系统分区

    rk3568的openwrt根文件系统分区大小如何修改 xff1f 1 rootfs大小取决于rk356x config的配置 xff0c 默认CONFIG TARGET ROOTFS PARTSIZE 61 512 xff0c 如果需要修
  • 除法(Division)

    Division Time Limit 3000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description Write a program th
  • 最大乘积(Maximum Product)

    Maximum Product Time Limit 3000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description Problem D M
  • 分数拆分(Fractions Again?!)

    Fractions Again Time Limit 3000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description Problem A F
  • 二叉树(Tree Recovery)

    Tree Recovery Time Limit 3000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description Little Valent
  • 骑士的移动(Knight Moves)

    Knight Moves Time Limit 3000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description A friend of yo
  • 单词(Play On Words)

    分析 首先需对欧拉道路有所了解 存在欧拉道路的充分条件 xff1a 对于无向图而言 xff0c 如果一个无向图是连通的 xff0c 且最多只有两个奇点 xff08 顶点的度数为奇数 xff09 xff0c 则一定存在欧拉道路 如果有两个奇点
  • 成语接龙(Idiomatic Phrases Game)

    Idiomatic Phrases Game Problem Description Tom is playing a game called Idiomatic Phrases Game An idiom consists of seve
  • DijKstra算法(单源最短路径)

    原文转载自 xff1a 梦醒潇湘love 转载原文是为了方便自己学习 xff0c 也希望能让更多读者在需要的情况下学到更多的知识 Dijkstra xff08 迪杰斯特拉 xff09 算法是典型的最短路径路由算法 xff0c 用于计算一个节
  • Floyd算法(最短路径)

    Floyd 算法允许图中有带负权值的边 xff0c 但不许有包含带负权值的边组成的回路 原文转载自 xff1a 梦醒潇湘love 上一篇文章我们通过迪杰斯特拉算法解决了从某个源点到其余各顶点的最短路径问题 从循环嵌套很容易得到此算法的时间复
  • 最爱的城市

    最爱的城市 时间限制 xff1a 1 秒 内存限制 xff1a 32 兆 特殊判题 xff1a 否 标签 Floyd最短路径 题目描述 一天小明捧着一本世界地图在看 xff0c 突然小明拿起笔 xff0c 将他最爱的那些城市标记出来 xff
  • Docker中配置Nginx多域名配置多个应用

    注意容器中是一个被隔离的空间 xff0c 可以理解为一个独立的服务器 xff0c 所以在做反向代理的时候 xff0c nginx配置不能使用localhost xff0c 可以使用link方式去访问其他容器 nginxa container
  • 八皇后问题(回溯法)

    八皇后问题 xff08 源于 刘汝佳的 算法竞赛入门经典 xff08 第2版 xff09 xff09 在棋盘上放置8个皇后 xff0c 使得它们互不攻击 xff0c 此时每个皇后的攻击范围为同行同列和同对角线 xff0c 要求找出所有解 x
  • 大整数的乘法

    大整数的乘法 xff08 这里主要讨论的是两个较大的数相乘的效率问题 xff0c 实际上并不是真正意义上的大数相乘 在java中有个BigInteger类已经可以储存大数 xff0c 并提供了大数相乘的方法了 xff09 分析 首先 xff
  • 2的次幂表示

    2的次幂表示 时间限制 xff1a 1 0s 内存限制 xff1a 512 0MB 问题描述 任何一个正整数都可以用2进制表示 xff0c 例如 xff1a 137的2进制表示为10001001 将这种2进制表示写成2的次幂的和的形式 xf
  • 扑克序列

    扑克序列 题目描述 标题 xff1a 扑克序列 A A 2 2 3 3 4 4 xff0c 一共4对扑克牌 请你把它们排成一行 要求 xff1a 两个A中间有1张牌 xff0c 两个2之间有2张牌 xff0c 两个3之间有3张牌 xff0c