- #include <iostream>
- #include <vector>
- using namespace std;
- int getMaxpos(vector<double> nums,int n)
- {
- int maxPos = 0;
- double tmp = nums.at(maxPos);
- for(int i = 1; i < n; i++)
- {
- if( nums.at(maxPos) < nums.at(i) )
- {
- maxPos = i;
- tmp = nums.at(i);
- }
- }
- return maxPos;
- }
- int getMinpos(vector<double> nums,int n)
- {
- int minPos = 0;
- double tmp = nums.at(minPos);
- for(int i = 1; i < n; i++)
- {
- if( nums.at(minPos) > nums.at(i) )
- {
- minPos = i;
- tmp = nums.at(i);
- }
- }
- return minPos;
- }
- int main()
- {
- int num;
- double max = 0;
- double min = 0;
- int maxPos = 0;
- int minPos = 0;
- double tt;
- vector<double> iNumAry;
- cin>>num;
- for(int i=0; i<num; i++)
- {
- cin>>tt;
- iNumAry.push_back(tt);
- }
- maxPos = getMaxpos(iNumAry,num);
- minPos = getMinpos(iNumAry,num);
- max = iNumAry.at(maxPos);
- min = iNumAry.at(minPos);
- int *count=new int[num];
- double *low=new double[num];
- double *high=new double[num];
- double avrGap=(max-min)/(num-1);
- for(int i=0;i<num;i++)
- {
- count[i] = 0;
- low[i] = max;
- high[i] = min;
- }
- int pos;
- for(int i=0; i<num; i++)
- {
- pos=int((iNumAry.at(i)-min)/avrGap)+1;
- if( pos == num ) pos -= 1;
- count[pos] ++;
- if( iNumAry.at(i) < low[pos] ) low[pos]=iNumAry.at(i);
- if( iNumAry.at(i) > high[pos] ) high[pos]=iNumAry.at(i);
- }
- double result = 0;
- double datHigh=high[1];
- double datTempGap;
- for(int i = 2; i < num; i++)
- {
- if(count[i])
- {
- datTempGap=low[i]-datHigh;
- if( result < datTempGap)
- result = datTempGap;
- datHigh=high[i];
- }
- }
- cout<<result<<endl;
- delete[] count;
- count=NULL;
- delete[] low;
- low=NULL;
- delete[] high;
- high=NULL;
- return 0;
- }
最大间隙问题
最新推荐文章于 2023-03-10 10:48:56 发布
1937

被折叠的 条评论
为什么被折叠?



