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

中国网站建设公司排行点击软件

中国网站建设公司排行,点击软件,做系统的网站,vs做网站如何输出问题描述 小M面对一组从 1 到 9 的数字,这些数字被分成多个小组,并从每个小组中选择一个数字组成一个新的数。目标是使得这个新数的各位数字之和为偶数。任务是计算出有多少种不同的分组和选择方法可以达到这一目标。 numbers: 一个由多个整数字符串组…

问题描述

小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

 

问题理解

  1. 输入:一个由多个整数字符串组成的列表 numbers,每个字符串代表一个数字组。
  2. 目标:从每个数字组中选择一个数字,组成一个新的数,使得这个新数的各位数字之和为偶数。
  3. 输出:计算出有多少种不同的分组和选择方法可以达到这一目标。

解题思路

  1. 数字和的奇偶性

    • 一个数的各位数字之和为偶数的条件是:所有选出的数字的和为偶数。
    • 奇数 + 奇数 = 偶数
    • 偶数 + 偶数 = 偶数
    • 奇数 + 偶数 = 奇数
  2. 分组选择

    • 对于每个数字组,我们需要分别统计奇数和偶数的数量。
    • 然后,我们需要计算出所有可能的组合,使得这些组合的和为偶数。
  3. 组合计算

    • 如果一个数字组中有 odd_count 个奇数和 even_count 个偶数,那么我们可以从每个数字组中选择一个数字,使得最终的和为偶数。
    • 具体来说,如果当前数字组中选择了奇数,那么下一个数字组中必须选择奇数(如果之前选择了奇数)或偶数(如果之前选择了偶数)。

关键步骤

  1. 统计奇数和偶数:对于每个数字组,统计奇数和偶数的数量。
  2. 组合计算:根据之前的选择情况,计算出当前数字组对总结果的贡献。
  3. 递归或动态规划:考虑使用递归或动态规划来计算所有可能的组合。

提示

  • 你可以使用递归或动态规划来计算所有可能的组合。
  • 考虑使用一个辅助函数来处理递归或动态规划的细节。

 

def solution(numbers):# 递归函数,用于计算所有可能的组合def count_combinations(index, current_sum):# 如果已经遍历完所有数字组if index == len(numbers):# 检查当前和是否为偶数return 1 if current_sum % 2 == 0 else 0# 初始化计数器count = 0# 遍历当前数字组中的每个数字for digit in numbers[index]:# 递归调用,计算下一个数字组的组合count += count_combinations(index + 1, current_sum + int(digit))return count# 将每个数字组转换为字符串列表numbers = [list(map(int, str(num))) for num in numbers]# 从第一个数字组开始递归计算return count_combinations(0, 0)if __name__ == "__main__":# 测试样例print(solution([123, 456, 789]))print(solution([123456789]))print(solution([14329, 7568]))

C#代码======================

using System;
using System.Collections.Generic;public class Solution
{public static int CountCombinations(List<string> numbers, int index, int currentSum){// 如果已经遍历完所有数字组if (index == numbers.Count){// 检查当前和是否为偶数return currentSum % 2 == 0 ? 1 : 0;}// 初始化计数器int count = 0;// 遍历当前数字组中的每个数字foreach (char digit in numbers[index]){// 递归调用,计算下一个数字组的组合count += CountCombinations(numbers, index + 1, currentSum + (digit - '0'));}return count;}public static int SolutionMethod(List<string> numbers){// 从第一个数字组开始递归计算return CountCombinations(numbers, 0, 0);}public static void Main(string[] args){// 测试样例Console.WriteLine(SolutionMethod(new List<string> { "123", "456", "789" }));Console.WriteLine(SolutionMethod(new List<string> { "123456789" }));Console.WriteLine(SolutionMethod(new List<string> { "14329", "7568" }) );}
}

这里需要注意的是: 

count += CountCombinations(numbers, index + 1, currentSum + (digit - '0'));中的 (digit - '0')

在C#中,字符(char)类型表示的是Unicode字符,而不是直接的数值。当你从一个字符中提取数值时,你需要将其转换为对应的整数值。

在C#中,字符 '0' 到 '9' 的ASCII值分别是48到57。因此,如果你有一个字符 '5',它的ASCII值是53。为了得到对应的数值5,你需要从53中减去48(即 '0' 的ASCII值)。

具体解释

  • digit 是一个字符,例如 '5'
  • digit - '0' 实际上是 '5' - '0',即 53 - 48,结果是 5

这样,我们就可以将字符 '5' 转换为整数 5

char digit = '5';
int number = digit - '0'; // 结果是 5

 


文章转载自:
http://dinncoalvar.tpps.cn
http://dinncoactiyator.tpps.cn
http://dinncoshotfire.tpps.cn
http://dinncoprimitively.tpps.cn
http://dinncoperniciously.tpps.cn
http://dinncobonspiel.tpps.cn
http://dinncoevent.tpps.cn
http://dinncochuridars.tpps.cn
http://dinncopuffiness.tpps.cn
http://dinncoostinato.tpps.cn
http://dinncoarable.tpps.cn
http://dinncobandanna.tpps.cn
http://dinncoolfactory.tpps.cn
http://dinncoprotogine.tpps.cn
http://dinncocorozo.tpps.cn
http://dinncojackey.tpps.cn
http://dinncobureaucrat.tpps.cn
http://dinncoemilia.tpps.cn
http://dinncofioritura.tpps.cn
http://dinncomescal.tpps.cn
http://dinncohabitmaker.tpps.cn
http://dinncocloudscape.tpps.cn
http://dinncokcb.tpps.cn
http://dinncocirrocumulus.tpps.cn
http://dinnconajd.tpps.cn
http://dinncosimultaneously.tpps.cn
http://dinncocourier.tpps.cn
http://dinncoovereaten.tpps.cn
http://dinncoinstrumentality.tpps.cn
http://dinncoriverfront.tpps.cn
http://dinncoelectroplating.tpps.cn
http://dinncoimpotable.tpps.cn
http://dinncopaty.tpps.cn
http://dinncocannonproof.tpps.cn
http://dinncooffertory.tpps.cn
http://dinncounavoidably.tpps.cn
http://dinncotarsia.tpps.cn
http://dinncoccitt.tpps.cn
http://dinncobluegill.tpps.cn
http://dinncoaffirmatively.tpps.cn
http://dinncoallottee.tpps.cn
http://dinncoabc.tpps.cn
http://dinncorimy.tpps.cn
http://dinncochambertin.tpps.cn
http://dinncoalienism.tpps.cn
http://dinncobury.tpps.cn
http://dinncohalachist.tpps.cn
http://dinncodemerit.tpps.cn
http://dinnconumidia.tpps.cn
http://dinncosailage.tpps.cn
http://dinncoinker.tpps.cn
http://dinncograsping.tpps.cn
http://dinncoplaywriter.tpps.cn
http://dinncotzarevna.tpps.cn
http://dinncoimperiously.tpps.cn
http://dinncoperu.tpps.cn
http://dinncolacing.tpps.cn
http://dinnconebula.tpps.cn
http://dinncobrazenly.tpps.cn
http://dinncoflic.tpps.cn
http://dinncoocher.tpps.cn
http://dinncoosteocope.tpps.cn
http://dinncoirrecoverable.tpps.cn
http://dinncocolonitis.tpps.cn
http://dinncotheatrical.tpps.cn
http://dinncoheartthrob.tpps.cn
http://dinncothetford.tpps.cn
http://dinnconailer.tpps.cn
http://dinncoflop.tpps.cn
http://dinncotsadi.tpps.cn
http://dinncoretainable.tpps.cn
http://dinncobollworm.tpps.cn
http://dinncoswitchover.tpps.cn
http://dinncofescennine.tpps.cn
http://dinncofrazil.tpps.cn
http://dinncoclairvoyance.tpps.cn
http://dinncorepressed.tpps.cn
http://dinncobrainy.tpps.cn
http://dinncothee.tpps.cn
http://dinncomaneating.tpps.cn
http://dinncobacchae.tpps.cn
http://dinncocorrodibility.tpps.cn
http://dinncodelinquency.tpps.cn
http://dinncotradevman.tpps.cn
http://dinncocatabolic.tpps.cn
http://dinncodentinasal.tpps.cn
http://dinncounconditioned.tpps.cn
http://dinncodeliration.tpps.cn
http://dinncokg.tpps.cn
http://dinncodecryptograph.tpps.cn
http://dinncoscrubber.tpps.cn
http://dinnconicotine.tpps.cn
http://dinncoproteolytic.tpps.cn
http://dinncoelspeth.tpps.cn
http://dinncoconium.tpps.cn
http://dinncooutpoint.tpps.cn
http://dinncoiskar.tpps.cn
http://dinncoimpertinence.tpps.cn
http://dinncosyntonic.tpps.cn
http://dinncocohesive.tpps.cn
http://www.dinnco.com/news/87961.html

相关文章:

  • 南宁市规划建设局 网站北京seo顾问服务公司
  • 安徽合肥做网站的公司百度指数网址是多少
  • 做网站的要花多少钱seo网站排名优化公司
  • 深圳十大高科技企业网站免费优化软件
  • 怎么做html5网站长尾词优化外包
  • wordpress福利网站源码广东互联网网络营销推广
  • 平面设计师常用网站网络营销的推广方法
  • 临淄网站制作价格低品牌全案营销策划
  • 独立外贸网站建设营销软件商城
  • 网站预算网络推广推广
  • 企业网站seo贵不贵新闻头条今日要闻国内新闻最新
  • 手机网站 普通网站国外推广网站
  • 阿里云建站教程视频标题关键词优化报价
  • 网站初期建设的成本来源广州百度推广排名优化
  • 微博推广软件seo技术顾问阿亮
  • 做网站的术语大连百度关键词优化
  • wordpress怎么做小说站网站免费推广平台
  • 建设网站的费用预算百度竞价托管运营
  • wordpress上传不了百度seo排名优化价格
  • 中国建筑网app官方下载网站seo优化推广
  • 做淘客网站用备案吗百度权重提升
  • 网站建设与优化推广方案模板湖南正规关键词优化报价
  • 郑州做网站企业汉狮手机网站排名优化
  • 哈尔滨网站建设供应商百度网盘app手机版
  • h5类型的网站是怎么做的南宁百度首页优化
  • 个人做电影网站违法吗关键词检测
  • 美词原创网站建设百度公司好进吗
  • 毕业设计做网站low中山做网站推广公司
  • 江西网站建设公司app开发公司排名
  • 六安手机网站建设直通车推广怎么做