How to find the product of the elements of the array, which are located between the maximum and minimum elements?
Closed due to the fact that the question is too common for participants Vlad from Moscow , Denis , aleksandr barakin , Harry , Kromster 30 Nov '16 at 3:58 .
Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See βHow to ask a good question?β For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .
- And if all elements are equal? And if there are a lot of minimal elements? - VladD
- @VladD, this is the second stage, do not rush, we will see these questions with you)))) - isnullxbh
- @VladD can immediately link to questions about the long multiplication of numbers? And it does not fit in __ int128 - pavel
- one@pavel: Hm. We need the product from the minimum to the current element, the current product from the current minimum to the current maximum, and the current minimum and current maximum. It seems enough. If so, you can write quite readable. - VladD
- 6@Dasha Novikova Maximum and minimum elements can be many. Moreover, the range between the maximum and minimum elements may differ from the range between the minimum and maximum elements. Therefore, specify the conditions of the problem. For example, a task might sound like this: to find the product of elements in the range from the first minimum to the last maximum element, or in some other way. - Vlad from Moscow
|
4 answers
The solution is not the best, not the best, but try:
int data[] = { 2, 4, 5, 2, -3, 5, 2, 33, 5, 3, -45, 2 }; std::pair<int *, int *> pr = std::minmax_element( std::begin( data ), std::end( data ) ); if( std::distance( std::begin( data ), pr.second ) < std::distance( std::begin( data ), pr.first ) ) std::swap( pr.first, pr.second ); if( pr.first != pr.second && ++pr.first != pr.second ) { int res = std::accumulate( pr.first, pr.second, 1, std::multiplies<int>() ); } if (pr.second < pr.first), not? - Qwertiy β¦- @Qwertiy, there is a prefix operator ++, itβs not just there) - isnullxbh
- I actually wanted to replace this:
if( std::distance( std::begin( data ), pr.second ) < std::distance( std::begin( data ), pr.first ) ), not second condition. - Qwertiy β¦ - However, in the second you can also
if(++pr.first < pr.second)? - Qwertiy β¦ - @Qwertiy, I apologize, did not understand you correctly. Perhaps it will be acceptable here, I just donβt even know, is it ok to compare the addresses? - isnullxbh
|
My decision raises other questions, but nonetheless.
#include "stdafx.h" #include <iostream> #include <conio.h> int main() { int n = 0; std::cout << "n = "; std::cin >> n; int* mass = new int[n]; for (int i(0); i < n; i++) std::cin >> mass[i]; int max = 0, min = 0; for (int i(0); i < n; i++) { if (i == 0 && mass[i] == mass[max]) max = i; else if (mass[i]> mass[max]) max = i;// ΠΈΡΠ΅ΠΌ Π½ΠΎΠΌΠ΅Ρ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° if (i == 0 && mass[i] == mass[min]) min = i; else if (mass[i] < mass[min]) min = i;// ΠΈΡΠ΅ΠΌ Π½ΠΎΠΌΠ΅Ρ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° } std::cout << "min = " << min << std::endl; std::cout << "max = " << max << std::endl; int proizvedenie = 1; if (max < min) { for (int i = max + 1; i < min; i++) proizvedenie *= mass[i];// ΠΏΡΠΎΠΈΠ·Π²ΠΎΠ΄ΠΈΠΌ ΡΠΌΠ½ΠΎΠΆΠ΅Π½ΠΈΠ΅ Π² Π·Π°Π΄Π°Π½Π½ΠΎΠΌ Π΄ΠΈΠ°ΠΏΠ°Π·ΠΎΠ½Π΅ } else if (min < max) { for (int i = min + 1; i < max; i++) proizvedenie *= mass[i]; } std::cout << proizvedenie; _getch(); return 0; } - if you first write the maximum element, it does not count - Dasha Novikova
- @DashaNovikova, try to play with the values, for example, set
max = n-1;instead of zero. Experiment. - Semyon Shelukhin - @DashaNovikova, with minor changes. - Semyon Shelukhin
|
Calculations for "all occasions" :)
The code searches for one and more than one result, if any. After the search - there are examples of sorting the results according to various criteria.
Online version here - http://ideone.com/4CGmoo
#include <algorithm> #include <iostream> #include <vector> ////////////////////////////////////////////////////////////////// // ΠΡΠ΅ΠΌ ΠΏΡΠΎΠΈΠ·Π²Π΅Π΄Π΅Π½ΠΈΠ΅ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ² ΠΌΠ°ΡΡΠΈΠ²Π°, ΠΊΠΎΡΠΎΡΡΠ΅ ΡΠ°ΡΠΏΠΎΠ»ΠΎΠΆΠ΅Π½Ρ ΠΌΠ΅ΠΆΠ΄Ρ // ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΡΠΌ ΠΈ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΡΠΌ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ°ΠΌΠΈ // // ΠΠΎΠΏΠΎΠ»Π½ΠΈΡΠ΅Π»ΡΠ½ΠΎ (ΡΠ΅Π³ΠΎ Π½Π΅ ΡΡΠΎΡΠ½Π΅Π½ΠΎ Π² Π·Π°Π΄Π°Π½ΠΈΠΈ): // // 1. ΠΠ΅ ΠΈΡΠ΅ΠΌ Π² ΠΎΠ±ΡΠ°ΡΠ½ΠΎΠΌ ΠΏΠΎΡΡΠ΄ΠΊΠ΅ (ΠΎΡ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ³ΠΎ ΠΊ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΠΎΠΌΡ) // 2. ΠΠΎΠ·ΠΌΠΎΠΆΠ½ΡΠ΅ Π²Π°ΡΠΈΠ°Π½ΡΡ Π·Π°Π½ΠΎΡΠΈΠΌ Π² Π²Π΅ΠΊΡΠΎΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ² ΠΈ ΠΈΡ
// ΡΠ°Π·Π½ΠΎΠ²ΡΡΡΠ΅ΡΠΊΠΈ ΡΠΎΡΡΠΈΡΡΠ΅ΠΌ Π² ΠΏΠΎΡΡΠ΄ΠΊΠ΅ Π½ΡΠΆΠ½ΠΎΠ³ΠΎ ////////////////////////////////////////////////////////////////// typedef std::pair<std::size_t,std::size_t> PairType; typedef std::vector<PairType> ResType; void FuncMul(std::vector<int> Vec, ResType &Res) { // Π² Π²Π΅ΠΊΡΠΎΡΠ΅ Π΄Π»ΠΈΠ½ΠΎΠΉ ΠΌΠ΅Π½ΡΡΠ΅ 3-Ρ
ΠΈΡΠΊΠ°ΡΡ Π½Π΅ΡΠ΅Π³ΠΎ if (Vec.size()<3) throw std::runtime_error("Π½ΠΈΡΠ΅Π³ΠΎ Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½ΠΎ"); // ΠΎΠΏΡΠ΅Π΄Π΅Π»ΡΠ΅ΠΌΡΡ Ρ ΠΌΠΈΠ½ΠΈΠΌΡΠΌΠ°ΠΌΠΈ ΠΈ ΠΌΠ°ΠΊΡΠΈΠΌΡΠΌΠ°ΠΌΠΈ auto Min = *std::min_element(Vec.begin(),Vec.end()); auto Max = *std::max_element(Vec.begin(),Vec.end()); // ΠΈΡΠ΅ΠΌ Π²ΡΠ΅ ΠΏΠ°ΡΡ ΠΌΠ°Ρ
-min std::size_t I = 0, S = Vec.size(), X; int M = 0; while(I<S) { if (M == 0) { if (Vec[I]==Max) { M=1; X=I; } } else { if (Vec[I]==Min) { if (I>X+1) Res.push_back({X,I}); M=0; } else if (Vec[I]==Max) X=I; } I++; } if(Res.size()<1) throw std::runtime_error("Π½ΠΈΡΠ΅Π³ΠΎ Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½ΠΎ"); } int main() { std::vector<int> Vec = {3,-4,1,3,4,9,1,-4,9,9,2,-3,-4,9,1}; ResType Res; try { FuncMul(Vec,Res); // Π»ΡΠΌΠ±Π΄Π° Π²ΡΠ²ΠΎΠ΄Π° Π½Π° ΠΏΠ΅ΡΠ°ΡΡ auto PrnPair = [&](const PairType &P) { std::cout << P.first << ":" << P.second << " = " << std::accumulate(Vec.begin()+P.first+1, Vec.begin()+P.second, 1, std::multiplies<int>()) << std::endl; }; // Π»ΡΠΌΠ±Π΄Π° ΡΠΎΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΏΠΎ Π²Π΅Π»ΠΈΡΠΈΠ½Π΅ ΠΏΡΠΎΠΈΠ·Π²Π΅Π΄Π΅Π½ΠΈΡ auto SortMul = [&] (const PairType &P1, const PairType &P2) { return std::accumulate(Vec.begin()+P1.first+1, Vec.begin()+P1.second, 1, std::multiplies<int>()) < std::accumulate(Vec.begin()+P2.first+1, Vec.begin()+P2.second, 1, std::multiplies<int>()); }; // Π»ΡΠΌΠ±Π΄Π° ΡΠΎΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΏΠΎ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²Ρ ΠΌΠ½ΠΎΠΆΠΈΠΌΡΡ
auto SortDis = [&] (const PairType &P1, const PairType &P2) { return P1.second - P1.first < P2.second-P2.first; }; // 1) ΠΏΠ΅ΡΠ°ΡΡ "ΡΡΡΠΎΠ³ΠΎ" Π½Π΅ΡΠΎΡΡΠΈΡΠΎΠ²Π°Π½Π½ΠΎΠ³ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° -------------------------------------------- for(const auto &i:Res) PrnPair(i); std::cout << "=========================" << std::endl; // 2) ΠΏΠ΅ΡΠ°ΡΡ ΠΎΡΡΠΎΡΡΠΈΡΠΎΠ²Π°Π½Π½ΠΎΠ³ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΠΏΠΎ Π²ΠΎΠ·ΡΠ°ΡΡΠ°Π½ΠΈΡ Π²Π΅Π»ΠΈΡΠΈΠ½Ρ ΠΏΡΠΎΠΈΠ·Π²Π΅Π΄Π΅Π½ΠΈΡ ---------------- std::sort(Res.begin(),Res.end(),SortMul); for(const auto &i:Res) PrnPair(i); std::cout << "=========================" << std::endl; // 3) ΠΏΠ΅ΡΠ°ΡΡ ΠΎΡΡΠΎΡΡΠΈΡΠΎΠ²Π°Π½Π½ΠΎΠ³ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΠΏΠΎ ΡΠ±ΡΠ²Π°Π½ΠΈΡ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²Π° ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ² Π² ΠΏΡΠΎΠΈΠ·Π²Π΅Π΄Π΅Π½ΠΈΠΈ ---- std::sort(Res.begin(),Res.end(),SortDis); for(const auto &i:Res) PrnPair(i); std::cout << "=========================" << std::endl; } catch(std::runtime_error &E) { std::cout << "ΠΡΠΈΠ±ΠΊΠ°: " << E.what() << std::endl; } catch(...) { std::cout << "ΠΡΠΈΠ±ΠΊΠ°: ΡΡΠΎ-ΡΠΎ ΠΏΠΎΡΠ»ΠΎ Π½Π΅ ΡΠ°ΠΊ!" << std::endl; } return 0; } |
#include <algorithm> #include <iostream> #include <vector> int FuncMul(std::vector<int> Vec) { if (Vec.size()>2) { std::sort(Vec.begin(), Vec.end(), std::less<int>()); return std::accumulate(Vec.begin()+1, Vec.end()-1, 1, std::multiplies<int>()); } return 0; } int main() { std::vector<int> Vec = {9,1,2,3,4,1,1,-4}; std::cout << "FuncMul: " << FuncMul(Vec); return 0; } - "which are located between" - sorting does not match the condition. - Qwertiy β¦
- nothing has been said about sorting, and if it is impossible, then it is possible - Majestio
- @Majestio, are you serious? It is necessary to find the value for the current order, and not for the one that will turn out after sorting. Therefore, no sorting is out of the question. - isnullxbh
- Then indicate this in the question that order is important. - Majestio
- @Majestio, understand, the emphasis is on finding the same minimal and maximal element in the input set - however, different variants of their location are possible, which complicates the task. And so, it becomes much more primitive. - isnullxbh
|
