分治法找出数组中第二大数
2014-11-19 16:50
344 查看
#include <span style="font-family: Arial, Helvetica, sans-serif;"><iostream></span> using namespace std; class Res { public: int max; int secmax; }; Res maxnum(int *a,int first,int last) { int mid; Res res; if (last-first==1) { if (a[first] > a[last]) { res.max = a[first]; res.secmax = a[last]; } else { res.max = a[last]; res.secmax = a[first]; } return res; } if (last == first) { return res; } else { mid = (first+last)>>1; if (maxnum(a, first, mid).max > maxnum(a, mid, last).max) { res.max =maxnum(a, first, mid).max; res.secmax = maxnum(a, mid, last).max; } else { res.max = maxnum(a, mid, last).max; res.secmax = maxnum(a, first, mid).max; } return res; } } int main(int argc , const char * argv[]) { int a[10] ={1,2,3,9,5,6,7,8,4,0}; cout< return 0; }
相关文章推荐
- 找出数组中第二大数
- 找出整数数组中第二大数
- C++通过自定义函数找出一个整数数组中第二大数的方法
- 如何快速找出一个数组中最大数和第二大数
- Linux_C练习:快速找出一个数组中的最大数、第二大数。(笔试题)
- 找出数组中第二大数
- [基础题]1.快速找出一个数组中的最大数、第二大数。
- 快速找出数组中的最大数和第二大数
- 快速找出一个数组中的最大数和第二大数
- 找出一个整数数组中的第二大数
- 一次遍历找出整型数组中第二大数
- 快速找出一个数组中的最大数、第二大数。
- 写一个函数,找出一个整数数组中的第二大数
- // 对于一个数组返回第二大数
- 给定一个长度为N的数组,找出一个最长的单调自增子序列(不一定连续,但是顺序不能乱). 第二解
- 寻找数组中第二大数
- 函数功能:传入一个数组,返回数组中第二大数。已知数组中的对象都是数字构成的字符串,且任意两元素不相等。 如传入:@[@"1",@"2",@"3",@"4"],返回@"3"
- 数据结构——算法之(034)(编写一个函数求一个数组中的第二大数)
- 输出数组中第二大数
- 写一个函数找出一个整数数组中,第二大的数