Cod sursa(job #1417718)

Utilizator Toast97Calin Farcas Toast97 Data 10 aprilie 2015 20:57:51
Problema Semne Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <fstream>
#include <cstdlib>
#include <time.h>

using namespace std;

ifstream f ("semne.in");
ofstream g ("semne.out");

char sol[50005];
long long nr[50005];

int main()
{
    srand (time (0));

    long long n, s, curent = 0, poz;

    f >> n >> s;

    for (int i = 0; i < n; i ++)
    {
        f >> nr[i];
        curent += nr[i];
        sol[i] = '+';
    }

    while (curent != s)
    {
        poz = rand () % n;

        if (sol[poz] == '+' && curent > s)
        {
            sol[poz] = '-';
            curent -= 2 * nr[poz];
        }
        else if (sol[poz] == '-' && curent < s)
        {
            sol[poz] = '+';
            curent += 2 * nr[poz];
        }
    }

    g << sol;

    f.close ();
    g.close ();
    return 0;
}