算24点
最编程
2024-02-04 11:54:27
...
算24点
1. 题目/题目地址
题目地址:https://www.luogu.com.cn/problem/P1236
题目地址:https://www.luogu.com.cn/problem/P1236
2. 题目解析
1. 这道题直接采用暴力搜索即可。
2. 因为每次都是两个数参与运算,因此我们要从序列中任取两个数。选取的时候要包含所有的情况,不能遗漏。也不能重复(当一个序列存在多个数时,不能将下标相同的数进行反复的运算)。例如:1 2 3 4 我们选取(1,2)(1,3)(1,4)(2,3)(2,4)(3,4)。可能你会问,为什么不考虑(2,1)这种?因为做四则运算的时候,会考虑。
3. 每次选取两个数参与运算之后,原序列的数变成了n-2,运算之后会得到一个新数字,将新数字加入到序列,结果就为n-1。换句话说,每次选择两个数参与运算,都会将问题规模从n->n-1。(从n个数的算24点问题->从n-1个数的算24点问题)。当n=1时,我们需要判断序列中的第一个数是否为24即可。
4. 对于选取的两个数,我们要对其做四则运算。
4.1 对于加法来讲,只需要做一次。(a+b = b+a)
4.2 对于乘法来讲,只需要做一次。(a*b = b*a)
4.3 对于减法来讲,需要做两次。(a-b != b - a)
4.4 对于除法来讲,需要做两次。(a/b != b/a)
可能你会问,为什么不考虑括号?因为,括号只会影响两个数运算的先后次序,但是我们选取两个数时,已经考虑了所有的情况,因此不需要考虑括号。
我们选取两个数进行运算之前,要将没有参与运算的数存放在一个新数组中,将运算之后的结果放在新数组的最后一个位置上。之后进行递归。
5. 这道题需要输出运算的式子,因此当我们算出结果为24时,我们可以通过回溯,将结果存储在char的二维数组中。之后逆序输出即可。
6. 这道题有很多的坑点,需要解决(剪枝):
6.1 当输出算式时,两个操作数:大数在前,小数在后。
6.2 输出24时,一定是最后的结果为24,因此当中间结果运算也为24时,我们需要忽略这种情况。
6.3 运算的结果不可以为0或负数。
6.4 除法时,我们只需要考虑整除的情况。
3. 代码
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int arr[4];
char equations[100010][100];
int index = 0;
bool count24(int array[], int n){
if(n == 1){
if(array[0] == 24){
return true;
}else{
return false;
}
}else{
if(array[n-1] == 24){
return false;
}
}
//选两个数array[i]和array[j],要参与运算(不重复且要包含所有的情况)
for(int i = 0; i < n-1; i ++){
for(int j = i + 1; j < n; j ++){
int cnt = 0;
int temp[4] = {0};
//把其他的数放到数组中
for(int k = 0; k < n; k ++){
if(k != i && k != j){
temp[cnt++] = array[k];
}
}
//加
temp[cnt] = array[i] + array[j];
if(temp[cnt] > 0) {
if (count24(temp, n - 1)) {
if(array[i] > array[j]) {
sprintf(equations[index++], "%d+%d=%d\n", array[i], array[j], temp[cnt]);
}else {
sprintf(equations[index++], "%d+%d=%d\n", array[j], array[i], temp[cnt]);
}
return true;
}
}
//减
temp[cnt] = array[i] - array[j];
if(temp[cnt] > 0) {
if (count24(temp, n - 1)) {
sprintf(equations[index++], "%d-%d=%d\n", array[i], array[j], temp[cnt]);
return true;
}
}
temp[cnt] = array[j] - array[i];
if(temp[cnt] > 0) {
if (count24(temp, n - 1)) {
sprintf(equations[index++], "%d-%d=%d\n", array[j], array[i], temp[cnt]);
return true;
}
}
//乘
temp[cnt] = array[i] * array[j];
if(temp[cnt] > 0) {
if (count24(temp, n - 1)) {
if(array[i] > array[j]) {
sprintf(equations[index++], "%d*%d=%d\n", array[i], array[j], temp[cnt]);
}else{
sprintf(equations[index++], "%d*%d=%d\n", array[j], array[i], temp[cnt]);
}
return true;
}
}
//除
if(array[i] >= array[j] && array[i] % array[j] == 0){
temp[cnt] = array[i] / array[j];
if(temp[cnt] > 0) {
if (count24(temp, n - 1)) {
sprintf(equations[index++], "%d/%d=%d\n", array[i], array[j], temp[cnt]);
return true;
}
}
}
if(array[j] >= array[i] && array[j] % array[i] == 0){
temp[cnt] = array[j] / array[i];
if(temp[cnt] > 0) {
if (count24(temp, n - 1)) {
sprintf(equations[index++], "%d/%d=%d\n", array[j], array[i], temp[cnt]);
return true;
}
}
}
}
}
return false;
}
int main(){
for(int i = 0; i < 4; i ++){
scanf("%d",&arr[i]);
}
if(!count24(arr,4)){
printf("No answer!\n");
}else{
for(int i = index - 1; i >= 0; i --){
printf("%s",equations[i]);
}
}
return 0;
}
4. 致谢
[1] 上图的课件来自于中国科学院大学马丙鹏老师的计算机算法设计与分析课程。
[2] 本题思路参考https://www.bilibili.com/video/BV12E411E7u9/?spm_id_from=333.337.search-card.all.click&vd_source=a642bb3ddc5b706845426dc41d73fbda
上一篇: 24点计算
下一篇: 详尽的苹果开发者账号申请步骤指南
推荐阅读
-
35 岁实现财务*,腾讯程序员手握2300万提前退休?-1000万房产、1000万腾讯股票、加上300万的现金,一共2300万的财产。有网友算了一笔账,假设1000万的房产用于自住,剩下1300万资产按照平均税后20-50万不等进行计算,大约花上26-60年左右的时间才能赚到这笔钱。也就是说,普通人可能奋斗一辈子,才能赚到这笔钱。在很多人还在为中年危机而惶惶不可终日的时候,有的人的35岁,就已经安全着陆,试问哪个打工人不羡慕?但问题是有这样财富积累必然有像样的实力做靠山。没有人可以不劳而获。 看到这里,肯定有人说,那么对于普通人来说,卷可能真就成了唯一的出路。但是卷也有轻松的卷,“偷懒”的卷法,对于程序员而言,刨除掉一时无法改掉的开会传统占用的大部分时间,如何把有限的时间和精力放在真正重要的架构设计、需求设计上,而不是重复的造*,编码、改bug、手动测试。因此在科技改变生活的今天,学会使用AI工具成为程序员们的必备技能。 以全栈式全自动的软件开发工具飞算SoFlu软件机器人为例,作为全球首款面向微服务架构设计和最佳实践的软件机器人,SoFlu软件机器人改变了原来手工编码的作业模式,通过可视化拖拽方式以及参数配置就能实现等同于编写复杂代码的业务逻辑,在设计业务逻辑时就完成了微服务应用开发,做到“业务即图,图即代码”,实现“软件开发,十倍提效”。 而作为飞算SoFlu软件机器人的一个重要组成部分,FuncGPT(慧函数)支持所有类型函数创建。通过自然语言描述Java函数需求,实时生成高质量、高可读性的Java函数代码。生成代码可直接复制到IDEA,或一键导入Java全自动开发工具函数库。 FuncGPT(慧函数)具有五大核心能力: ● 自然语言:通过自然语言即可生成函数,降低软件开发门槛。 ● 秒级函数开发:全自动秒级开发函数,秒级完成,效率千倍提升。 ● 一站式函数开发应用:函数生成即可直接应用,效率、安全有保障。 ● 质量可靠:生成的函数代码符合业界规范、具备良好的可读性,符合最优解。 ● 灵活生成:可以根据开发人员的具体需求生成并支持调整和修改。 以“将文件流转换为File对象”这个需求为例,以下是FuncGPT(慧函数)秒级生成的代码: / 类名:FileConverter // 函数名:convertStreamToFile // 函数功能:将文件流转换为File对象 // POM依赖包:无 import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; public class FileConverter { /** * 将文件流转换为File对象 * @param inputStream 文件流 * @param filePath 文件路径 * @param fileName 文件名 * @return 转换后的File对象 * @throws IOException 如果发生I/O异常 */ public static File convertStreamToFile(InputStream inputStream, String filePath, String fileName) throws IOException { File file = new File(filePath + File.separator + fileName); // 创建File对象 try (FileOutputStream outputStream = new FileOutputStream(file)) { // 创建文件输出流 byte buffer = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { // 从文件流读取数据并写入文件 outputStream.write(buffer, 0, bytesRead); } } return file; // 返回转换后的File对象 } } // 函数示例 // 将文件流转换为File对象示例 // 入参:inputStream,文件流 // 入参:filePath,文件路径 // 入参:fileName,文件名 // 出参:file,转换后的File对象 // 调用示例: // InputStream inputStream = new FileInputStream("example.txt"); // String filePath = "C:\\Users\\User\\Documents"; // String fileName = "example.txt"; // File file = FileConverter.convertStreamToFile(inputStream, filePath, fileName); // System.out.println(file.getAbsolutePath); // 输出结果:例如,将文件流转换为File对象后,文件的绝对路径为:C:\Users\User\Documents\example.txt // 则输出结果为:C:\Users\User\Documents\example.txt 通过分析,不难发现以上代码:
-
四轴软件姿态解算
-
【注释谢希仁-算网五版:第四章 网络层(一)
-
C 语言 - 算牌 24 点游戏
-
算24
-
什么算单元测试?谁应该负责编写单元测试?微软和谷歌这么说 ......
-
展望 2024 年:中国人工智能算力能否引发高性能计算和大型模型训练的新革命?
-
openEuler郑州用户组成立!openEuler与hyperfusion携手共建河南地区用户生态 - 开幕致辞 超融合操作系统业务总经理、openEuler委员会成员蒋振华先生为本次活动致辞。 在本次活动的致辞中,他提到,作为openEuler社区早期的成员,超融合见证了openEuler从成立到在各行业商业落地,再到跨越生态拐点的过程,感谢openEuler提供了一个全产业链共同创新的平台,共同推动创新技术的商业落地。 同时,本次活动得到了郑州市郑东新区大数据管理局、郑州中原科技城投资服务局的大力支持。 郑东新区大数据管理局曹光远 在活动致辞中表示,openEuler的应用和*应用设施的深度优化,为郑东新区数字化转型提供了安全、可靠、高性能的技术基础;郑州中原科技城招商服务局王林表示,郑东新区欢迎所有openEuler生态相关企业扎根当地,围绕openEuler社区共同发展,形成合力。 openEuler社区及运维功能介绍 openEuler技术委员会委员胡峰 openEuler技术委员会委员胡峰先生在本次活动中介绍了openEuler社区目前发展的整体情况,并重点从技术层面介绍了openEuler的运维功能。 openEuler 晚会 胡峰先生介绍智能运维工具 A-Ops 和 openEuler gala、 阿波罗 Apollo、智能漏洞管理解决方案等新功能,以及涵盖各种运维场景的精品运维组件。在*交流环节,许多用户就目前使用的 openEuler 在*交流环节,许多用户就自己在使用openEuler过程中遇到的一些问题与胡峰先生进行了进一步的交流。 软硬结合,构建多样化算力操作系统 Hyperfusion 基于 openEuler 的基础上,结合自身软硬件技术积累,推出了富讯服务器操作系统 FusionOS FusionOS. FusionOS 首席架构师张海亮 分享了 FusionOS FusionOS首席架构师张海亮分享了FusionOS的软硬件协同优势、卓越的性能和可靠性,以及FusionOS在金融、运营商、*、互联网等行业的实践案例,引起了众多用户的兴趣,分享结束后,不少参会者就FusionOS的特点向讲师提问并进行了交流。
-
先算后套的编译原则 C++ 实现
-
我国古代数学家张丘建在《算经》一书中提出了这样一道数学题:鸡值五钱,鸡母值三钱,鸡雏值一钱。百钱买百鸡,问鸡、鸡母、鸡雏几何?