java求众数_17088 分治法求众数(备忘录)

17088 分治法求众数(备忘录)

所属分类:数学计算

开发工具:C/C++

文件大小:14KB

下载次数:1

上传日期:2019-01-07 23:44:18

上 传 者:Wayne_xuan

说明:  17088 分治法求众数(必做) 时间限制:1000MS 内存限制:65535K 提交次数:0 通过次数:0 题型: 编程题 语言: G++ GCC VC JAVA Description 给定含有n个元素的多重集合S,每个元素在S中出现的次数称为该元素的重数。多重集S中重数最大的元素称为 众数。例如,S={1,2,2,2,3,5}。多重集S的众数是2,其重数为3。 求众数方法很多,现要求你用分治算法来试一试,并分析其效率。 编程任务:对于给定的由n个自然数组成的多重集S,采用分治算法编程计算S的众数及其重数。 输入格式 第1行多重集S中元素个数n;接下来的一行为集合S,有n个自然数。( n < 100000 ) 输出格式 结果输出:输出2个数,第1个数为众数,第2个为其重数。 当有多个同样重数的众数,优先输出数值更小的众数。 输入样例 6 1 2 2 2 3 5 输出样例 2 3

((17088 divide and conquer method the modal number (will do) Time limit: 1000MS Memory Limit: 65535K Submit: 0 Passes: 0 Questions: Programming language title: G++ GCC VC JAVA Description Given n elements containing multiple set S, the number of each element appears in the S called the multiplicity of the element. Multiple sets the maximum number of S in heavy elements called Plural. For example, S = {1,2,2,2,3,5}. Majority of multiple sets of S is 2, its weight is three. Many seek the mode method, you are now required to use the divide and conquer algorithm to try and analyze their efficiency. Programming tasks: for a given by n natural numbers consisting of multiple sets S, using all programmed to calculate the number of divide and conquer algorithm S and multiplicity. Input format The first line multi-set number of elements in S n an act the next set S, there is a natural number n. (N <100000) Output Format Results output: 2 number, the first number ))

文件列表:[举报垃圾]

17088 分治法求众数(备忘录), 0 , 2018-10-16

17088 分治法求众数(备忘录)\17088 分治法求众数.cbp, 1157 , 2018-10-16

17088 分治法求众数(备忘录)\17088 分治法求众数.depend, 126 , 2018-10-16

17088 分治法求众数(备忘录)\17088 分治法求众数.layout, 358 , 2018-10-16

17088 分治法求众数(备忘录)\bin, 0 , 2018-10-16

17088 分治法求众数(备忘录)\bin\Debug, 0 , 2018-10-16

17088 分治法求众数(备忘录)\bin\Debug\17088 分治法求众数.exe, 29353 , 2018-10-16

17088 分治法求众数(备忘录)\main.cpp, 456 , 2018-10-16

17088 分治法求众数(备忘录)\obj, 0 , 2018-10-16

17088 分治法求众数(备忘录)\obj\Debug, 0 , 2018-10-16

17088 分治法求众数(备忘录)\obj\Debug\main.o, 2649 , 2018-10-16

近期下载者:

相关文件:

收藏者:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
分治算法是一种基于分治策略的算法,它将问题分解为若干个子问题,然后递归地解决这些子问题,最后将这些子问题的解合并起来得到原问题的解。在众数问题时,可以使用分治算法来解决。 众数即为出现次数最多的数。要解一个数组中的众数,可以将数组分为两个部分,分别出左右两个部分的众数,然后再将两个众数进行比较,选出其中的众数分治算法众数的步骤如下: 1. 将数组分为左右两个部分,分别递归解左右两个部分的众数。 2. 如果左右两个部分的众数相同,则直接返回这个众数。 3. 如果左右两个部分的众数不同,则统计左右两个部分中各自的众数出现的次数,选出次数更多的那个数作为整个数组的众数Java代码实现如下: ```java public int majorityElement(int[] nums) { if (nums.length == 1) { return nums[0]; } int mid = nums.length / 2; int[] left = Arrays.copyOfRange(nums, 0, mid); int[] right = Arrays.copyOfRange(nums, mid, nums.length); int leftMajority = majorityElement(left); int rightMajority = majorityElement(right); if (leftMajority == rightMajority) { return leftMajority; } int leftCount = count(nums, leftMajority); int rightCount = count(nums, rightMajority); return leftCount > rightCount ? leftMajority : rightMajority; } private int count(int[] nums, int num) { int count = 0; for (int i = 0; i < nums.length; i++) { if (nums[i] == num) { count++; } } return count; } ``` 这样,就可以使用分治算法来解决众数问题了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值