网站构思,线上销售渠道,渝北区两江新区,上海人才信息网官网数字分组求偶数和
1.问题描述
问题描述
小M面对一组从 1 到 9 的数字#xff0c;这些数字被分成多个小组#xff0c;并从每个小组中选择一个数字组成一个新的数。目标是使得这个新数的各位数字之和为偶数。任务是计算出有多少种不同的分组和选择方法可以达到这一目标。
n…数字分组求偶数和
1.问题描述
问题描述
小M面对一组从 1 到 9 的数字这些数字被分成多个小组并从每个小组中选择一个数字组成一个新的数。目标是使得这个新数的各位数字之和为偶数。任务是计算出有多少种不同的分组和选择方法可以达到这一目标。
numbers: 一个由多个整数字符串组成的列表每个字符串可以视为一个数字组。小M需要从每个数字组中选择一个数字。
例如对于[123, 456, 789]14个符合条件的数为147 149 158 167 169 248 257 259 268 347 349 358 367 369。 测试样例
样例1 输入numbers [123, 456, 789] 输出14 样例2 输入numbers [123456789] 输出4 样例3 输入numbers [14329, 7568] 输出10 2.思路与题解
问题理解
你需要从每个数字组中选择一个数字使得这些数字的和为偶数。问题的核心在于如何判断一个数的和是否为偶数。
数据结构选择
输入是一个整数数组每个整数可以转换为字符串来处理。你需要遍历每个数字组并从每个组中选择一个数字。
算法步骤
转换输入将整数数组转换为字符串数组以便于处理每个数字组中的单个数字。递归遍历使用递归函数来遍历每个数字组并从每个组中选择一个数字。和的判断在递归过程中累加当前选择的数字并在递归的终止条件处判断累加和是否为偶数。计数如果累加和为偶数则计数加一。
2.4代码框架
Java
public class Main {public static int solution(int[] numbers) {// 将整数数组转换为字符串数组String[] numStrings new String[numbers.length];for (int i 0; i numbers.length; i) {numStrings[i] String.valueOf(numbers[i]);}// 调用递归函数进行计算return countEvenSumCombinations(numStrings, 0, 0);}// 递归函数用于计算符合条件的组合数private static int countEvenSumCombinations(String[] numStrings, int index, int currentSum) {// 如果已经遍历完所有数字组if (index numStrings.length) {// 检查当前和是否为偶数if (currentSum % 2 0) {return 1;} else {return 0;}}int count 0;// 遍历当前数字组中的每个数字for (char digit : numStrings[index].toCharArray()) {// 将字符转换为数字int num digit - 0;// 递归调用选择下一个数字组count countEvenSumCombinations(numStrings, index 1, currentSum num);}return count;}public static void main(String[] args) {// 测试用例System.out.println(solution(new int[]{123, 456, 789}) 14);System.out.println(solution(new int[]{123456789}) 4);System.out.println(solution(new int[]{14329, 7568}) 10);}
}C
#include iostream
#include vector
#include stringint solution(std::vectorint numbers) {int count 0;// 遍历每个数字组for (int num : numbers) {std::string numStr std::to_string(num);// 遍历数字组中的每个数字for (char digit : numStr) {// 计算和int sum 0;// 计算各位数字之和// 判断和是否为偶数if (sum % 2 0) {count;}}}return count;
}int main() {// You can add more test cases herestd::cout (solution({123, 456, 789}) 14) std::endl;std::cout (solution({123456789}) 4) std::endl;std::cout (solution({14329, 7568}) 10) std::endl;return 0;
}Python
def solution(numbers):def is_even_sum(digits):# 检查一组数字的和是否为偶数return sum(digits) % 2 0def count_even_sum_combinations(index, current_digits):# 如果已经遍历完所有数字组if index len(numbers):# 检查当前组合的和是否为偶数if is_even_sum(current_digits):return 1return 0count 0# 遍历当前数字组中的每个数字for digit in str(numbers[index]):# 选择当前数字并递归处理下一个数字组count count_even_sum_combinations(index 1, current_digits [int(digit)])return count# 从第一个数字组开始递归计算return count_even_sum_combinations(0, [])if __name__ __main__:# 你可以添加更多测试用例print(solution([123, 456, 789]) 14)print(solution([123456789]) 4)print(solution([14329, 7568]) 10)Golang
package mainimport fmtfunc solution(numbers []int) int {// 辅助函数用于判断一个数是否为偶数isEven : func(num int) bool {return num%2 0}// 递归函数用于遍历所有可能的选择var dfs func(index int, currentSum int) intdfs func(index int, currentSum int) int {// 如果已经遍历完所有数字组if index len(numbers) {// 检查当前和是否为偶数if isEven(currentSum) {return 1}return 0}count : 0// 将当前数字组转换为字符串numStr : fmt.Sprintf(%d, numbers[index])// 遍历当前数字组中的每个数字for i : 0; i len(numStr); i {// 将字符转换为数字digit : int(numStr[i] - 0)// 递归处理下一个数字组count dfs(index1, currentSumdigit)}return count}// 从第一个数字组开始递归return dfs(0, 0)
}func main() {// 你可以添加更多测试用例fmt.Println(solution([]int{123, 456, 789}) 14)fmt.Println(solution([]int{123456789}) 4)fmt.Println(solution([]int{14329, 7568}) 10)
}2.5一些疑难的代码解释
递归函数的设计确保递归函数能够正确地遍历每个数字组并累加当前选择的数字。和的判断在递归的终止条件处判断累加和是否为偶数。计数如果累加和为偶数则计数加一。
3.欢迎大佬们关注或莅临本渣的一些个人website
gitee https://gitee.com/xiao-chenago githubhttps://github.com/cool-icu0 语雀https://www.yuque.com/icu0 csdnhttps://cool-icu.blog.csdn.net/ 文章转载自: http://www.morning.mlbn.cn.gov.cn.mlbn.cn http://www.morning.trffl.cn.gov.cn.trffl.cn http://www.morning.plcyq.cn.gov.cn.plcyq.cn http://www.morning.kdrjd.cn.gov.cn.kdrjd.cn http://www.morning.ywzqk.cn.gov.cn.ywzqk.cn http://www.morning.dlurfdo.cn.gov.cn.dlurfdo.cn http://www.morning.mttqp.cn.gov.cn.mttqp.cn http://www.morning.hqllx.cn.gov.cn.hqllx.cn http://www.morning.kjyqr.cn.gov.cn.kjyqr.cn http://www.morning.drbd.cn.gov.cn.drbd.cn http://www.morning.kqzt.cn.gov.cn.kqzt.cn http://www.morning.c7513.cn.gov.cn.c7513.cn http://www.morning.rdzlh.cn.gov.cn.rdzlh.cn http://www.morning.mqwnz.cn.gov.cn.mqwnz.cn http://www.morning.wjhdn.cn.gov.cn.wjhdn.cn http://www.morning.gl-group.cn.gov.cn.gl-group.cn http://www.morning.qdrhf.cn.gov.cn.qdrhf.cn http://www.morning.ntzfj.cn.gov.cn.ntzfj.cn http://www.morning.gqtxz.cn.gov.cn.gqtxz.cn http://www.morning.skrrq.cn.gov.cn.skrrq.cn http://www.morning.pybqq.cn.gov.cn.pybqq.cn http://www.morning.nrftd.cn.gov.cn.nrftd.cn http://www.morning.qwlml.cn.gov.cn.qwlml.cn http://www.morning.dkbsq.cn.gov.cn.dkbsq.cn http://www.morning.zlqyj.cn.gov.cn.zlqyj.cn http://www.morning.yktwr.cn.gov.cn.yktwr.cn http://www.morning.dyxlj.cn.gov.cn.dyxlj.cn http://www.morning.pzrnf.cn.gov.cn.pzrnf.cn http://www.morning.bpttm.cn.gov.cn.bpttm.cn http://www.morning.wflpj.cn.gov.cn.wflpj.cn http://www.morning.xckdn.cn.gov.cn.xckdn.cn http://www.morning.gybnk.cn.gov.cn.gybnk.cn http://www.morning.nlygm.cn.gov.cn.nlygm.cn http://www.morning.zyrp.cn.gov.cn.zyrp.cn http://www.morning.fthcn.cn.gov.cn.fthcn.cn http://www.morning.rwrn.cn.gov.cn.rwrn.cn http://www.morning.ljqd.cn.gov.cn.ljqd.cn http://www.morning.sgnxl.cn.gov.cn.sgnxl.cn http://www.morning.zjcmr.cn.gov.cn.zjcmr.cn http://www.morning.mjgxl.cn.gov.cn.mjgxl.cn http://www.morning.dqcpm.cn.gov.cn.dqcpm.cn http://www.morning.ltrms.cn.gov.cn.ltrms.cn http://www.morning.thbnt.cn.gov.cn.thbnt.cn http://www.morning.jzfrl.cn.gov.cn.jzfrl.cn http://www.morning.gqfjb.cn.gov.cn.gqfjb.cn http://www.morning.jcypk.cn.gov.cn.jcypk.cn http://www.morning.ygth.cn.gov.cn.ygth.cn http://www.morning.bkpbm.cn.gov.cn.bkpbm.cn http://www.morning.drytb.cn.gov.cn.drytb.cn http://www.morning.kstgt.cn.gov.cn.kstgt.cn http://www.morning.gwkjg.cn.gov.cn.gwkjg.cn http://www.morning.nrzkg.cn.gov.cn.nrzkg.cn http://www.morning.msgnx.cn.gov.cn.msgnx.cn http://www.morning.rwls.cn.gov.cn.rwls.cn http://www.morning.glcgy.cn.gov.cn.glcgy.cn http://www.morning.mgzjz.cn.gov.cn.mgzjz.cn http://www.morning.jqkjr.cn.gov.cn.jqkjr.cn http://www.morning.pxwzk.cn.gov.cn.pxwzk.cn http://www.morning.btrfm.cn.gov.cn.btrfm.cn http://www.morning.sypzg.cn.gov.cn.sypzg.cn http://www.morning.bchhr.cn.gov.cn.bchhr.cn http://www.morning.xtkw.cn.gov.cn.xtkw.cn http://www.morning.znknj.cn.gov.cn.znknj.cn http://www.morning.hmmtx.cn.gov.cn.hmmtx.cn http://www.morning.srndk.cn.gov.cn.srndk.cn http://www.morning.mqxzh.cn.gov.cn.mqxzh.cn http://www.morning.cmfkp.cn.gov.cn.cmfkp.cn http://www.morning.gcqkb.cn.gov.cn.gcqkb.cn http://www.morning.dkslm.cn.gov.cn.dkslm.cn http://www.morning.lpgw.cn.gov.cn.lpgw.cn http://www.morning.dtrcl.cn.gov.cn.dtrcl.cn http://www.morning.lxfyn.cn.gov.cn.lxfyn.cn http://www.morning.ckzjl.cn.gov.cn.ckzjl.cn http://www.morning.fhqdb.cn.gov.cn.fhqdb.cn http://www.morning.kyhnl.cn.gov.cn.kyhnl.cn http://www.morning.bygyd.cn.gov.cn.bygyd.cn http://www.morning.ylkkh.cn.gov.cn.ylkkh.cn http://www.morning.qpnmd.cn.gov.cn.qpnmd.cn http://www.morning.cjqcx.cn.gov.cn.cjqcx.cn http://www.morning.dkzrs.cn.gov.cn.dkzrs.cn