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
    }
相关推荐
2401_858286111 分钟前
125.【C语言】数据结构之归并排序递归解法
c语言·开发语言·数据结构·算法·排序算法·归并排序
掘金-我是哪吒8 分钟前
分布式微服务系统架构第156集:JavaPlus技术文档平台日更-Java线程池使用指南
java·分布式·微服务·云原生·架构
亲爱的非洲野猪34 分钟前
Kafka消息积压的多维度解决方案:超越简单扩容的完整策略
java·分布式·中间件·kafka
wfsm37 分钟前
spring事件使用
java·后端·spring
guygg8837 分钟前
基于matlab的FIR滤波器
开发语言·算法·matlab
双叶8361 小时前
(C++)学生管理系统(正式版)(map数组的应用)(string应用)(引用)(文件储存的应用)(C++教学)(C++项目)
c语言·开发语言·数据结构·c++
微风粼粼1 小时前
程序员在线接单
java·jvm·后端·python·eclipse·tomcat·dubbo
缘来是庄1 小时前
设计模式之中介者模式
java·设计模式·中介者模式
源代码•宸1 小时前
C++高频知识点(二)
开发语言·c++·经验分享
ysh98881 小时前
PP-OCR:一款实用的超轻量级OCR系统
算法