Cod sursa(job #729680)

Utilizator w3.playerCostescu Ionut w3.player Data 29 martie 2012 19:56:50
Problema Loto Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 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; k=1; j=n-k;
			for (i=0; i<6; i++)
			{
				l[i]=v[j];
				sl+=l[i];
				if (sl>=s && i!=5)
				{
					sl-=l[i];
					i--; j--;
					if (j==-1)
					{
						i--;
						k++; sl-=l[i];
						j=n-k;
					}
				}
			}
			ofstream outFile("loto.out");
			for (i=0; i<6; i++)
				outFile << l[i] << " ";
			outFile << '\n';
			outFile.close();
		}
	return 0;
}