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
    }
相关推荐
暮冬-  Gentle°1 分钟前
C++代码依赖分析
开发语言·c++·算法
糯诺诺米团3 分钟前
C++多线程打包成so给JAVA后端(Ubuntu)<3>
java·开发语言·c++
2301_763891954 分钟前
泛型编程与STL设计思想
开发语言·c++·算法
j_xxx404_6 分钟前
蓝桥杯基础--进制转换
开发语言·数据结构·c++·算法·职场和发展·蓝桥杯
SimonKing7 分钟前
还在本地硬扛大模型?试试 Ollama Cloud,顺便把 OpenCode 也升级了
java·后端·程序员
xjdkxnhcoskxbco9 分钟前
Kotlin Lambda 变量捕获
android·开发语言
沐知全栈开发10 分钟前
ASP TextStream
开发语言
人间打气筒(Ada)11 分钟前
go实战案例:如何在 Go-kit 和 Service Meh 中进行服务注册与发现?
开发语言·后端·golang·istio·go-kit
myloveasuka12 分钟前
[Java]双列集合
java
Blasit13 分钟前
Qt 程序打包,运行提示找不到或无法加载平台插件 qwindows.dll
开发语言·windows·qt