每日一题(22) - 寻找数组中的最大值和最小值
来源:互联网 发布:node.js在线 编辑:程序博客网 时间:2024/06/04 19:28
题目来自编程之美
题目:寻找数组中的最大值和最小值
要求:要求比较次数最少
分析:使用分治法解决,详细见点击打开链接
代码:
#include <iostream>#include <assert.h>using namespace std;void FindMaxAndMin(int arr[],int nStart,int nEnd,int& nMax,int& nMin){assert(arr != NULL && nStart <= nEnd && nStart >= 0 && nEnd >= 0);int nLeftMax = 0;int nLeftMin = 0;int nRightMax = 0;int nRightMin = 0;if (nStart == nEnd - 1)//区间相差为1{if (arr[nStart] > arr[nEnd]){nMax = arr[nStart];nMin = arr[nEnd];}else{nMax = arr[nEnd];nMin = arr[nStart];}return;}if (nStart == nEnd)//区间相差为0{nMin = nMax = arr[nStart];return;}int nMid = (nStart + nEnd)/2;FindMaxAndMin(arr,nStart,nMid,nLeftMax,nLeftMin);FindMaxAndMin(arr,nMid + 1,nEnd,nRightMax,nRightMin);//分别比较两部分的最大值和最小值,求出整体的最大值和最小值nMax = max(nLeftMax,nRightMax);nMin = min(nLeftMin,nRightMin);}int main(){int nMin = 0;int nMax = 0;int arr[8] = {4,-3,5,-2,-1,2,6,-2};FindMaxAndMin(arr,0,7,nMax,nMin);cout<<"Max: "<<nMax<<endl;//6cout<<"Min: "<<nMin<<endl;//-3int arr1[8] = {4,-3,5,-11,4,-2,5,-2};FindMaxAndMin(arr1,0,7,nMax,nMin);cout<<"Max: "<<nMax<<endl;//5cout<<"Min: "<<nMin<<endl;//-11int arr2[8] = {-3,-5,-8,-2,-11,-12,-13,-1};FindMaxAndMin(arr2,0,7,nMax,nMin);cout<<"Max: "<<nMax<<endl;//-1cout<<"Min: "<<nMin<<endl;//-13system("pause");return 1;}
题目来自编程之美
题目:寻找数组中的最大值和次大值
要求:要求比较次数最少
分析:使用分治法解决,详细见点击打开链接
注意:如果最大值在序列中重复出现,无法找到次大值。
举例:如序列:4,-3,5,-11,4,-2,5,-2, 最大值5出现了两次,此时算法输出5和5,而不是5,4。
代码:
#include <iostream>#include <assert.h>using namespace std;void FindMaxAndSecMax(int arr[],int nStart,int nEnd,int& nMax,int& nSecMax){assert(arr != NULL && nStart <= nEnd && nStart >= 0 && nEnd >= 0);int nLeftMax = 0;int nLeftSecMax = 0;int nRightMax = 0;int nRightSecMax = 0;if (nStart == nEnd - 1)//区间相差为1{if (arr[nStart] > arr[nEnd]){nMax = arr[nStart];nSecMax = arr[nEnd];}else{nMax = arr[nEnd];nSecMax = arr[nStart];}return;}if (nStart == nEnd)//区间相差为0{nSecMax = nMax = arr[nStart];return;}int nMid = (nStart + nEnd)/2;FindMaxAndSecMax(arr,nStart,nMid,nLeftMax,nLeftSecMax);FindMaxAndSecMax(arr,nMid + 1,nEnd,nRightMax,nRightSecMax);//分别比较两部分的最大值和次大值,求出整体的最大值和次大值if (nLeftMax > nRightMax){nMax = nLeftMax;if (nLeftSecMax > nRightMax){nSecMax = nLeftSecMax;}else{nSecMax = nRightMax;}}else{nMax = nRightMax;if (nRightSecMax > nLeftMax){nSecMax = nRightSecMax;}else{nSecMax = nLeftMax;}}}int main(){int nMin = 0;int nMax = 0;int arr[8] = {4,-3,5,-2,-1,2,6,-2};FindMaxAndSecMax(arr,0,7,nMax,nMin);cout<<"Max: "<<nMax<<endl;//6cout<<"Min: "<<nMin<<endl;//5int arr1[8] = {4,-3,5,-11,4,-2,5,-2};FindMaxAndSecMax(arr1,0,7,nMax,nMin);cout<<"Max: "<<nMax<<endl;//5cout<<"Min: "<<nMin<<endl;//5int arr2[8] = {-3,-5,-8,-2,-11,-12,-13,-1};FindMaxAndSecMax(arr2,0,7,nMax,nMin);cout<<"Max: "<<nMax<<endl;//-1cout<<"Min: "<<nMin<<endl;//-2system("pause");return 1;}
- 每日一题(22) - 寻找数组中的最大值和最小值
- 寻找数组中的最大值和最小值
- 2.10 寻找数组中的最大值和最小值
- 寻找数组中的最大值和最小值
- 寻找数组中的最大值和最小值
- 寻找数组中的最大值和最小值
- 寻找数组中的最大值和最小值
- 寻找数组中的最大值和最小值
- 寻找数组中的最大值和最小值
- 2.10 寻找数组中的最大值和最小值
- 寻找数组中的最大值和最小值
- 寻找数组中的最大值和最小值
- 1、寻找数组中的最大值和最小值
- 寻找数组中的最大值和最小值
- 寻找数组中的最大值和最小值
- 寻找数组中的最大值和最小值
- 2.10 寻找数组中的最大值和最小值
- 寻找数组中的最大值和最小值
- OpenMP中的任务调度
- Oracle分组函数之CUBE魅力
- Oracle 数据泵使用详解
- shell 中if作比较
- c语言指针习题分享
- 每日一题(22) - 寻找数组中的最大值和最小值
- 怎样查看最消耗资源的sql语句 与 ora-00031:session marked for kill
- 关于android的一些默认的系统style和theme
- unning LotusScript Agent that calls a Java Library Generates the following Error: java.lang.Security
- cognos 年累计 同比年累计 DB2保存小数位2位
- 谨此日记纪念大学的最后一次考试
- Java中的==和equals区别
- win7 做 无线路由器
- iOS实现反射机制