java编程解小学生一年级竞赛题

抖音教学视频

目录

1、题目三角形加起来为10


大纲

1、题目三角形加起来为10

连接:小学一年级数学竞赛练习题3套,有点难度! 第16题

此方法不是最优解,穷举法,比较暴力解决

主要给大家演示如何用编程去解决我们的实际问题,

java 复制代码
/**
 * @ClassName TopicTest
 * @Author chuige
 * @create 2024/1/14 10:33
 */
public class TopicTest {

    public static void main(String[] args) {
        new TopicTest().topic16();
    }

    /*
     * @Author 吹老师
     * @Description // 解 小学生一年级 最难的竞赛题
     * @Date 10:34 2024/1/14
     * @Param []
     * @return void
     **/
    private void topic16() {
        int[] a = {1, 2, 3, 4, 5, 6};
        int[] result = new int[6];
        topic16(a, 0, result);
        System.out.println("执行完成");
    }

    private void topic16(int[] a, int count, int[] result) {
        if (count == 6) {
            printResult(result);
            return;
        }
        for (int i = 0; i < 6; i++) {
            result[count] = i + 1;
            topic16(a, count + 1, result);
        }
    }

    private void printResult(int[] result) {
        int reuslt1 = result[0] + result[1] + result[2];
        int reuslt2 = result[2] + result[3] + result[4];
        int reuslt3 = result[4] + result[5] + result[0];
        if (reuslt1 != 10 || reuslt2 != 10 || reuslt3 != 10) {
            return;
        }
        StringBuilder stringBuilderResult = new StringBuilder();
        stringBuilderResult.append("结果:");
        Set<Integer> repeatSet = new HashSet<>();
        for (int temp : result) {
            if (repeatSet.contains(temp)) {
                return;
            }
            repeatSet.add(temp);
            stringBuilderResult.append(" " + temp);
        }
        System.out.println("满足条件的结果:" + stringBuilderResult.toString());

//        满足条件的结果:结果: 1 4 5 2 3 6
//        满足条件的结果:结果: 1 6 3 2 5 4
//        满足条件的结果:结果: 3 2 5 4 1 6
//        满足条件的结果:结果: 3 6 1 4 5 2
//        满足条件的结果:结果: 5 2 3 6 1 4
//        满足条件的结果:结果: 5 4 1 6 3 2
    }
相关推荐
Σdoughty几秒前
python第三次作业
开发语言·前端·python
老鼠只爱大米几秒前
LeetCode经典算法面试题 #114:二叉树展开为链表(递归、迭代、Morris等多种实现方案详细解析)
算法·leetcode·二叉树·原地算法·morris遍历·二叉树展开
是萧萧吖几秒前
每日一练——有效的括号
java·开发语言·javascript
MediaTea3 分钟前
Python:内置类型也是类对象
开发语言·python
gpldock2225 分钟前
Flutter App Templates Deconstructed: A 2025 Architectural Review
开发语言·javascript·flutter·wordpress
程序员欣宸6 分钟前
LangChain4j实战之十六:RAG (检索增强生成),Naive RAG
java·人工智能·ai·langchain4j
Ivanqhz7 分钟前
现代异构高性能计算(HPC)集群节点架构
开发语言·人工智能·后端·算法·架构·云计算·边缘计算
qq_3363139320 分钟前
javaweb-Maven
java·maven
Sayuanni%320 分钟前
数据结构_Map和Set
java·数据结构
Demon_Hao21 分钟前
Spring Boot开启虚拟线程ScopedValue上下文传递
java·spring boot·后端