当前位置: 首页 > news >正文

唐朝网站南阳网站排名优化费用

唐朝网站,南阳网站排名优化费用,网站定制公司蒙特,湖北百度关键词排名软件数字分组求偶数和 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
http://www.tj-hxxt.cn/news/247747.html

相关文章:

  • 如何做公司的网站广东网站营销seo费用
  • 旅游网站盈利模式怎么做网站推广方法有几个
  • 兼职做任务的网站淄博前信网络科技有限公司
  • 算命网站该怎样做设计上海设计公司
  • 网站建设前期如何做好市场定位分析昆明php网站建设
  • 我有域名怎么做网站网站建设项目报价单
  • 家装建材公司网站建设初中做历史的网站
  • wordpress 自定义逻辑网站怎样做优化调整
  • 馆陶专业做网站专业网站建设费用报价
  • 做黑枸杞的公司网站景区微网站 建设方案
  • 陕西省建设网官方网站织梦模板如何安装
  • 胶州建设信息网站简述网站的制作步骤
  • 开发一个网站要学什么软件网站建设销售应答技巧
  • 企业网站制作开发合肥网站建站报广告代理
  • 惠州学院网站建设无锡网站制作电话
  • 有哪些官方网站做的比较好枣庄建设网站
  • 合肥网络科技有限公司做网站建设局工资一年多少钱
  • 做淘宝客网站需要备案吗网站cms下载
  • 企业做淘宝客网站微博推广的优势
  • h5微网站建设多少钱东莞最近三天的新闻大事
  • 杭州市建设厅网站深圳网站建设电话
  • 嘉兴网站建设运营网题 做问卷的网站
  • 做网站的把网站写成一行企业网站建设熊掌号
  • 专业做网站厂家广告设计介绍
  • 免费手机网站空间申请三杰网站建设
  • 没有rss源的网站如何做rss订阅宁波网络推广联系方式
  • 商洛网站建设电话iis添加网站无法访问
  • 做好网站建设深圳专业建网站多少钱
  • 重庆技术网站建设中国的网站域名是什么
  • wordpress更改站点ps课堂网站