Java教程

多个长度集合拼接出想要长度的组合

本文主要是介绍多个长度集合拼接出想要长度的组合,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
多个长度集合拼接出想要长度的组合
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @program: world
 * @description: 多个长度集合拼接出想要长度的组合
 * @author: XinHai.Ma
 * @create: 2021-07-11 21:41
 */
@SuppressWarnings("all")
public class CountTests {

    private static final Logger logger = LoggerFactory.getLogger(CountTests.class);

    public static void main(String[] args) {
        // 8种长度模具
        int[] nums = new int[8];
        nums[0] = 10;
        nums[1] = 11;
        nums[2] = 12;
        nums[3] = 13;
        nums[4] = 14;
        nums[5] = 15;
        nums[6] = 7;
        nums[7] = 8;

        // 顺序模具
        int[] nums1 = new int[8];
        nums1[0] = 7;
        nums1[1] = 8;
        nums1[2] = 10;
        nums1[3] = 11;
        nums1[4] = 12;
        nums1[5] = 13;
        nums1[6] = 14;
        nums1[7] = 15;

        int totalLen1 = 36; // 40
        int totalLen2 = 36; // 42

            for (int j = 0; j < nums.length; j++) {
                if(j <= nums.length-4) {
                    int total = nums[j] + nums[j+1] + nums[j+2] + nums[j+3];
                    if(total <= totalLen2) {
                        logger.info("{}={}+{}+{}+{}", total, nums[j], nums[j+1], nums[j+2], nums[j+3]);
                    }
                }

                for(int i=j+2; i < nums.length; i++) {
                    if(i <= nums.length-3) {
                        int total = nums[j] + nums[i] + nums[i+1] + nums[i+2];
                        if(total <= totalLen2) {
                            logger.info("{}={}+{}+{}+{}", total, nums[j], nums[i], nums[i+1], nums[i+2]);
                        }
                    }
                }
            }


            for (int j = 0; j < nums.length; j++) {
                if(j <= nums.length-3) {
                    int total = nums[j] + nums[j+1] + nums[j+2];
                    if(total <= totalLen2) {
                        logger.info("{}={}+{}+{}", total, nums[j], nums[j+1], nums[j+2]);
                    }

                    int num = totalLen1 - total;
                    int num1 = totalLen2 - total;
                    for (int i1 = nums1.length-1; i1 >= 0; i1--) {
                        if(num >= nums1[i1] && ((nums1[i1]+total) <= totalLen1)) {
                            logger.info("{}={}+{}+{}+{}", total, nums[j], nums[j+1], nums[j+2], nums1[i1]);
                        }
                        if(num1 >= nums1[i1] && ((nums1[i1]+total) <= totalLen2)) {
                            logger.info("{}={}+{}+{}+{}", total, nums[j], nums[j+1], nums[j+2], nums1[i1]);
                        }
                    }
                }
                for(int i=j+2; i < nums.length; i++) {
                    if(i <= nums.length-2) {
                        int total = nums[j] + nums[i] + nums[i+1];
                        if(total <= totalLen1) {
                            logger.info("{}={}+{}+{}", total, nums[j], nums[i], nums[i+1]);
                        }

                        int num = totalLen1 - total;
                        int num1 = totalLen2 - total;
                        for (int i1 = nums1.length-1; i1 >= 0; i1--) {
                            if(num >= nums1[i1] && ((nums1[i1]+total) <= totalLen1)) {
                                logger.info("{}={}+{}+{}+{}", total, nums[j], nums[i], nums[i+1], nums1[i1]);
                            }
                            if(num1 >= nums1[i1] && ((nums1[i1]+total) <= totalLen2)) {
                                logger.info("{}={}+{}+{}+{}", total, nums[j], nums[i], nums[i+1], nums1[i1]);
                            }
                        }
                    }
                }
            }
    }

}
计算结果
23:10:26.420 [main] INFO com.maxinhai.world.CountTests - 33=10+11+12
23:10:26.423 [main] INFO com.maxinhai.world.CountTests - 35=10+12+13
23:10:26.423 [main] INFO com.maxinhai.world.CountTests - 32=10+15+7
23:10:26.423 [main] INFO com.maxinhai.world.CountTests - 25=10+7+8
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 25=10+7+8+11
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 25=10+7+8+11
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 25=10+7+8+10
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 25=10+7+8+10
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 25=10+7+8+8
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 25=10+7+8+8
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 25=10+7+8+7
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 25=10+7+8+7
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 36=11+12+13
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 33=11+15+7
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 26=11+7+8
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 26=11+7+8+10
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 26=11+7+8+10
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 26=11+7+8+8
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 26=11+7+8+8
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 26=11+7+8+7
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 26=11+7+8+7
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 34=12+15+7
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 27=12+7+8
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 27=12+7+8+8
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 27=12+7+8+8
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 27=12+7+8+7
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 27=12+7+8+7
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 35=13+15+7
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 28=13+7+8
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 28=13+7+8+8
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 28=13+7+8+8
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 28=13+7+8+7
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 28=13+7+8+7
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 36=14+15+7
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 29=14+7+8
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 29=14+7+8+7
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 29=14+7+8+7
23:10:26.424 [main] INFO com.maxinhai.world.CountTests - 30=15+7+8

 

 

 

这篇关于多个长度集合拼接出想要长度的组合的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!