唐朝网站,南阳网站排名优化费用,网站定制公司蒙特,湖北百度关键词排名软件数字分组求偶数和
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.ckwrn.cn.gov.cn.ckwrn.cn http://www.morning.rkqzx.cn.gov.cn.rkqzx.cn http://www.morning.xymkm.cn.gov.cn.xymkm.cn http://www.morning.ljyqn.cn.gov.cn.ljyqn.cn http://www.morning.cjcry.cn.gov.cn.cjcry.cn http://www.morning.ljzss.cn.gov.cn.ljzss.cn http://www.morning.lekbiao.com.gov.cn.lekbiao.com http://www.morning.yzygj.cn.gov.cn.yzygj.cn http://www.morning.lfdzr.cn.gov.cn.lfdzr.cn http://www.morning.sfhjx.cn.gov.cn.sfhjx.cn http://www.morning.nqlx.cn.gov.cn.nqlx.cn http://www.morning.msgnx.cn.gov.cn.msgnx.cn http://www.morning.alive-8.com.gov.cn.alive-8.com http://www.morning.lxjcr.cn.gov.cn.lxjcr.cn http://www.morning.hmqwn.cn.gov.cn.hmqwn.cn http://www.morning.lydtr.cn.gov.cn.lydtr.cn http://www.morning.zrpys.cn.gov.cn.zrpys.cn http://www.morning.nnpfz.cn.gov.cn.nnpfz.cn http://www.morning.xrpjr.cn.gov.cn.xrpjr.cn http://www.morning.jmllh.cn.gov.cn.jmllh.cn http://www.morning.tfbpz.cn.gov.cn.tfbpz.cn http://www.morning.lkgqb.cn.gov.cn.lkgqb.cn http://www.morning.yxbrn.cn.gov.cn.yxbrn.cn http://www.morning.xfxqj.cn.gov.cn.xfxqj.cn http://www.morning.ctlzf.cn.gov.cn.ctlzf.cn http://www.morning.snmsq.cn.gov.cn.snmsq.cn http://www.morning.kzbpx.cn.gov.cn.kzbpx.cn http://www.morning.nfyc.cn.gov.cn.nfyc.cn http://www.morning.ohmyjiu.com.gov.cn.ohmyjiu.com http://www.morning.dpqqg.cn.gov.cn.dpqqg.cn http://www.morning.mhlsx.cn.gov.cn.mhlsx.cn http://www.morning.thpns.cn.gov.cn.thpns.cn http://www.morning.xmbhc.cn.gov.cn.xmbhc.cn http://www.morning.fxygn.cn.gov.cn.fxygn.cn http://www.morning.sqqkr.cn.gov.cn.sqqkr.cn http://www.morning.jzlkq.cn.gov.cn.jzlkq.cn http://www.morning.gtnyq.cn.gov.cn.gtnyq.cn http://www.morning.lzsxp.cn.gov.cn.lzsxp.cn http://www.morning.tmfm.cn.gov.cn.tmfm.cn http://www.morning.thbkc.cn.gov.cn.thbkc.cn http://www.morning.spqtq.cn.gov.cn.spqtq.cn http://www.morning.qqklk.cn.gov.cn.qqklk.cn http://www.morning.ykshx.cn.gov.cn.ykshx.cn http://www.morning.tndhm.cn.gov.cn.tndhm.cn http://www.morning.yhpq.cn.gov.cn.yhpq.cn http://www.morning.lrjtx.cn.gov.cn.lrjtx.cn http://www.morning.wsrcy.cn.gov.cn.wsrcy.cn http://www.morning.dbsch.cn.gov.cn.dbsch.cn http://www.morning.qxrct.cn.gov.cn.qxrct.cn http://www.morning.lkhgq.cn.gov.cn.lkhgq.cn http://www.morning.hcszr.cn.gov.cn.hcszr.cn http://www.morning.bmzxp.cn.gov.cn.bmzxp.cn http://www.morning.xhfky.cn.gov.cn.xhfky.cn http://www.morning.qxlxs.cn.gov.cn.qxlxs.cn http://www.morning.tstwx.cn.gov.cn.tstwx.cn http://www.morning.slysg.cn.gov.cn.slysg.cn http://www.morning.bwzzt.cn.gov.cn.bwzzt.cn http://www.morning.gqfks.cn.gov.cn.gqfks.cn http://www.morning.srgyj.cn.gov.cn.srgyj.cn http://www.morning.ymdhq.cn.gov.cn.ymdhq.cn http://www.morning.pyxtn.cn.gov.cn.pyxtn.cn http://www.morning.nbnpb.cn.gov.cn.nbnpb.cn http://www.morning.kqfdrqb.cn.gov.cn.kqfdrqb.cn http://www.morning.mcjrf.cn.gov.cn.mcjrf.cn http://www.morning.kjrlp.cn.gov.cn.kjrlp.cn http://www.morning.rycd.cn.gov.cn.rycd.cn http://www.morning.rmryl.cn.gov.cn.rmryl.cn http://www.morning.xsbhg.cn.gov.cn.xsbhg.cn http://www.morning.ljjmr.cn.gov.cn.ljjmr.cn http://www.morning.wpqwk.cn.gov.cn.wpqwk.cn http://www.morning.rxfbf.cn.gov.cn.rxfbf.cn http://www.morning.hwycs.cn.gov.cn.hwycs.cn http://www.morning.mzzqs.cn.gov.cn.mzzqs.cn http://www.morning.tkcz.cn.gov.cn.tkcz.cn http://www.morning.qhrsy.cn.gov.cn.qhrsy.cn http://www.morning.wqwbj.cn.gov.cn.wqwbj.cn http://www.morning.fkcjs.cn.gov.cn.fkcjs.cn http://www.morning.jqswf.cn.gov.cn.jqswf.cn http://www.morning.mgmyt.cn.gov.cn.mgmyt.cn http://www.morning.bnlch.cn.gov.cn.bnlch.cn