Consiste en retornar la posición del elemento máximo(el mas grande) de un vector.
En caso de empate se tiene que devolver la posición minima.
Yo he hecho este codigo pero no me funciona bien, y nose por donde tirar ya, haber si alguien me puede ayudar porfavor.
Código:
#include <iostream>
#include <vector>
using namespace std;
int posicio_maxim(const vector<double>& v, int m){
int max = 0;
int pos_max;
bool empat = false;
for(int j = 0; j <=m and not(empat); ++j){
if(v[j] > max){
max = v[j];
pos_max = j;
} else if(v[j] == max) {
empat = true;
}
}
int min = max;
int pos_min;
for(int j = 0; j <= m and empat; ++j){
if(v[j] < min){
min = v[j];
pos_min = j;
}
}
if(empat)
return pos_min;
else
return pos_max;
}
int main(){
int n;
cin >> n;
vector<double> v(n);
for(int i = 0; i < n; ++i){
cin >> v[i];
}
int x;
cin >> x;
cout << posicio_maxim(v, x) << endl;
}
#include <vector>
using namespace std;
int posicio_maxim(const vector<double>& v, int m){
int max = 0;
int pos_max;
bool empat = false;
for(int j = 0; j <=m and not(empat); ++j){
if(v[j] > max){
max = v[j];
pos_max = j;
} else if(v[j] == max) {
empat = true;
}
}
int min = max;
int pos_min;
for(int j = 0; j <= m and empat; ++j){
if(v[j] < min){
min = v[j];
pos_min = j;
}
}
if(empat)
return pos_min;
else
return pos_max;
}
int main(){
int n;
cin >> n;
vector<double> v(n);
for(int i = 0; i < n; ++i){
cin >> v[i];
}
int x;
cin >> x;
cout << posicio_maxim(v, x) << endl;
}
Muchas gracias.
Adiós.










Autor


En línea


