Cod sursa(job #729876)

Utilizator w3.playerCostescu Ionut w3.player Data 30 martie 2012 17:03:48
Problema Loto Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <fstream>
#include <algorithm>
using namespace std;
int n,i,j,k;
long s,v[100],l[6],sl;

int main()
{
	ifstream inFile("loto.in");
	inFile >> n >> s;
	for (i=0; i<n; i++)
		inFile >> v[i];
	inFile.close();
	sort(v,v+n);
	if (v[n-1]*6<s)
	{
		ofstream outFile("loto.out");
		outFile << -1 << '\n';
		outFile.close();
	}
	else
		if (v[n-1]*6==s)
		{
			ofstream outFile("loto.out");
			outFile << v[n-1] << " " << v[n-1] << " " << v[n-1] << " " << v[n-1] << " " << v[n-1] << " " << v[n-1] << '\n';
			outFile.close();
		}
		else
		{	
			sl=0; j=0;
			for (k=n-1; k>=0; k--)
				for (i=j; i<6; i++)
				{
					sl+=v[k];
					if (sl<=s && s-sl>=v[0]*(5-i))
					{
						l[i]=v[k];
						j++;
					}
					else
					{
						sl-=v[k];
						i=6;
					}
				}
			ofstream outFile("loto.out");
			for (i=0; i<6; i++)
				outFile << l[i] << " ";
			outFile << '\n';
			outFile.close();
		}
	return 0;
}