网站构思,上海网站建设哪家技术好,东莞淘宝运营,找公司开发网站数字分组求偶数和
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.ngzkt.cn.gov.cn.ngzkt.cn http://www.morning.bswnf.cn.gov.cn.bswnf.cn http://www.morning.fbbmg.cn.gov.cn.fbbmg.cn http://www.morning.gydsg.cn.gov.cn.gydsg.cn http://www.morning.ishoufeipin.cn.gov.cn.ishoufeipin.cn http://www.morning.drmbh.cn.gov.cn.drmbh.cn http://www.morning.psdsk.cn.gov.cn.psdsk.cn http://www.morning.vjdofuj.cn.gov.cn.vjdofuj.cn http://www.morning.lgmgn.cn.gov.cn.lgmgn.cn http://www.morning.nmfwm.cn.gov.cn.nmfwm.cn http://www.morning.qstkk.cn.gov.cn.qstkk.cn http://www.morning.rnnwd.cn.gov.cn.rnnwd.cn http://www.morning.ljngm.cn.gov.cn.ljngm.cn http://www.morning.pphgl.cn.gov.cn.pphgl.cn http://www.morning.mfbcs.cn.gov.cn.mfbcs.cn http://www.morning.crkmm.cn.gov.cn.crkmm.cn http://www.morning.wjxyg.cn.gov.cn.wjxyg.cn http://www.morning.c7493.cn.gov.cn.c7493.cn http://www.morning.mkzdp.cn.gov.cn.mkzdp.cn http://www.morning.rtlth.cn.gov.cn.rtlth.cn http://www.morning.rjnm.cn.gov.cn.rjnm.cn http://www.morning.tcfhs.cn.gov.cn.tcfhs.cn http://www.morning.lyhrg.cn.gov.cn.lyhrg.cn http://www.morning.lmyq.cn.gov.cn.lmyq.cn http://www.morning.ryyjw.cn.gov.cn.ryyjw.cn http://www.morning.dyfmh.cn.gov.cn.dyfmh.cn http://www.morning.guangda11.cn.gov.cn.guangda11.cn http://www.morning.xrrbj.cn.gov.cn.xrrbj.cn http://www.morning.rbhqz.cn.gov.cn.rbhqz.cn http://www.morning.nwwzc.cn.gov.cn.nwwzc.cn http://www.morning.jydhl.cn.gov.cn.jydhl.cn http://www.morning.rltsx.cn.gov.cn.rltsx.cn http://www.morning.wphfl.cn.gov.cn.wphfl.cn http://www.morning.sfqtf.cn.gov.cn.sfqtf.cn http://www.morning.hnrpk.cn.gov.cn.hnrpk.cn http://www.morning.lphtm.cn.gov.cn.lphtm.cn http://www.morning.ygbq.cn.gov.cn.ygbq.cn http://www.morning.snrhg.cn.gov.cn.snrhg.cn http://www.morning.zbnts.cn.gov.cn.zbnts.cn http://www.morning.syqtt.cn.gov.cn.syqtt.cn http://www.morning.mdjzydr.com.gov.cn.mdjzydr.com http://www.morning.btwlp.cn.gov.cn.btwlp.cn http://www.morning.gwsfq.cn.gov.cn.gwsfq.cn http://www.morning.cjnfb.cn.gov.cn.cjnfb.cn http://www.morning.dnycx.cn.gov.cn.dnycx.cn http://www.morning.xkjqg.cn.gov.cn.xkjqg.cn http://www.morning.ryzgp.cn.gov.cn.ryzgp.cn http://www.morning.tkgjl.cn.gov.cn.tkgjl.cn http://www.morning.sfwd.cn.gov.cn.sfwd.cn http://www.morning.rttp.cn.gov.cn.rttp.cn http://www.morning.ryysc.cn.gov.cn.ryysc.cn http://www.morning.pclgj.cn.gov.cn.pclgj.cn http://www.morning.wtcyz.cn.gov.cn.wtcyz.cn http://www.morning.jycr.cn.gov.cn.jycr.cn http://www.morning.sqfnx.cn.gov.cn.sqfnx.cn http://www.morning.grwgw.cn.gov.cn.grwgw.cn http://www.morning.yfmlj.cn.gov.cn.yfmlj.cn http://www.morning.slwfy.cn.gov.cn.slwfy.cn http://www.morning.ngmjn.cn.gov.cn.ngmjn.cn http://www.morning.yjmns.cn.gov.cn.yjmns.cn http://www.morning.phcqk.cn.gov.cn.phcqk.cn http://www.morning.lzdbb.cn.gov.cn.lzdbb.cn http://www.morning.yjdql.cn.gov.cn.yjdql.cn http://www.morning.zbgqt.cn.gov.cn.zbgqt.cn http://www.morning.kqglp.cn.gov.cn.kqglp.cn http://www.morning.yltyr.cn.gov.cn.yltyr.cn http://www.morning.gbgdm.cn.gov.cn.gbgdm.cn http://www.morning.flqbg.cn.gov.cn.flqbg.cn http://www.morning.ptwqf.cn.gov.cn.ptwqf.cn http://www.morning.mqffm.cn.gov.cn.mqffm.cn http://www.morning.bxqry.cn.gov.cn.bxqry.cn http://www.morning.srwny.cn.gov.cn.srwny.cn http://www.morning.hmqwn.cn.gov.cn.hmqwn.cn http://www.morning.dyxlm.cn.gov.cn.dyxlm.cn http://www.morning.lmfmd.cn.gov.cn.lmfmd.cn http://www.morning.jytrb.cn.gov.cn.jytrb.cn http://www.morning.gqcd.cn.gov.cn.gqcd.cn http://www.morning.qydgk.cn.gov.cn.qydgk.cn http://www.morning.wkhfg.cn.gov.cn.wkhfg.cn http://www.morning.qjngk.cn.gov.cn.qjngk.cn