Cod sursa(job #2750012)

Utilizator linte_robertLinte Robert linte_robert Data 9 mai 2021 13:39:36
Problema Zeap Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.95 kb
#include <iostream>
#include <cstring>
#include <fstream>
#include <vector>
using namespace std;
int minim( vector < int > &x ){
    int minim;
    minim = x[0];
    for( int i = 0; i < x.size(); i++ ){
        if( x[i] < minim ) minim = x[i];
    }
    return minim;
}
int maxim( vector < int > &x ){
    int maxim;
    maxim = -1;
    for( int i = 0; i < x.size(); i++ ){
        if( x[i] > maxim ) maxim = x[i];
    }
    return maxim;
}
int cautare( vector < int > &x, int nr ){
    int ok = -1, j = -1;
    for( int i = 0; i < x.size() && ok == -1; i++ ){
        if( x[i] == nr ) {
            ok = 1;
            j = i;
        }
    }
    return j;
}
void stergere( vector < int > &x, int nr ){
    int i = cautare(x,nr);
    x[i] = x[ x.size()-1 ];
    x.pop_back();
}
int diferenta_minima ( vector < int > &x ){
    int d;
    if( x[1] > x[0] ) d = x[1] - x[0];
    else d = x[0] - x[1];
    for( int i = 0; i < x.size(); i++ ){
        for( int j = i+1; j < x.size(); j++ ){
            if( x[i] > x[j] && x[i]-x[j] < d ) d = x[i]-x[j];
            if( x[i] < x[j] && x[j]-x[i] < d ) d = x[j]-x[i];
        }
    }
    return d;
}

int main(){
    vector < int > x;
    string y;
    ifstream fin("zeap.in");
    ofstream fout("zeap.out");
    while( fin >> y ){
        if( y == "I" ){
            int z;
            fin >> z;
            x.push_back(z);
        }
        if( y == "S" ){
            int z;
            fin >> z;
            if( cautare(x,z) != -1 ){
                stergere( x, z );
            }
            else fout << -1 << endl;
        }
        if( y == "C" ){
            int z;
            fin >> z;
            if( cautare(x,z) != -1 ) fout << 1 << endl;
            else fout << 0 << endl;
        }
        if( y == "MAX" ){
            fout << maxim(x) - minim(x) <<endl;
        }
        if( y == "MIN" ){
            fout << diferenta_minima(x) << endl;
        }
    }
}