1 template <typename Comparable> 2 Comparable maxprod( const vector<Comparable>&v) 3 { 4 int i; 5 Comparable maxProduct = 1; 6 Comparable minProduct = 1; 7 Comparable maxCurrent = 1; 8 Comparable minCurrent = 1; 9 //Comparable t; 10 11 for( i=0; i< v.size() ;i++) 12 { 13 maxCurrent *= v[i]; 14 minCurrent *= v[i]; 15 if(maxCurrent > maxProduct) 16 maxProduct = maxCurrent; 17 if(minCurrent > maxProduct) 18 maxProduct = minCurrent; 19 if(maxCurrent < minProduct) 20 minProduct = maxCurrent; 21 if(minCurrent < minProduct) 22 minProduct = minCurrent; 23 if(minCurrent > maxCurrent) 24 swap(maxCurrent,minCurrent); 25 if(maxCurrent<1) 26 maxCurrent = 1; 27 //if(minCurrent>1) 28 // minCurrent =1; 29 } 30 return maxProduct; 31 }
算法题——最大连续子序列乘积
时间: 2024-11-08 08:09:10