Pagini recente » Diferente pentru teorema-chineza-a-resturilor intre reviziile 89 si 53 | Cod sursa (job #1778845) | Cod sursa (job #2275124) | Cod sursa (job #1754791) | Cod sursa (job #1732476)
#include <stdio.h>
#include <stdlib.h>
int a[100000001];
void quicksort(int arr[], int fst, int last)
{
int i,j,pivot,tmp;
if(fst<last)
{
pivot=fst;
i=fst;
j=last;
while(i<j)
{
while(arr[i]<=arr[pivot] && i<last)
i++;
while(arr[j]>arr[pivot])
j--;
if(i<j)
{
tmp=arr[i];
arr[i]=arr[j];
arr[j]=tmp;
}
}
tmp=arr[pivot];
arr[pivot]=arr[j];
arr[j]=tmp;
quicksort(arr,fst,j-1);
quicksort(arr,j+1,last);
}
}
int main()
{
FILE *pf,*pg;
pf=fopen("loto.in","r");
pg=fopen("loto.out","w");
int n,s,nr1,nr2;
fscanf(pf,"%d %d",&n,&s);
int v[n+1],i;
for (i=1; i<=n; i++)
fscanf(pf,"%d",&v[i]);
int j,k,p=0;
for (i=1; i<=n; i++)
for (j=1; j<=n; j++)
for (k=1; k<=n; k++)
a[++p]=v[i]+v[j]+v[k];
quicksort(a,1,p);
i=1;j=p;
while (i<=j)
{
if ((a[i]+a[j])==s)
{
nr1=a[i];
nr2=a[j];
//printf("%d %d",a[i],a[j]);
break;
}
else
{if (a[i]+a[j]<s)
i++;
else
j--;}
}
int ok=1;
for (i=1; i<=n; i++)
for (j=1; j<=n; j++)
for (k=1; k<=n; k++)
if (v[i]+v[j]+v[k]==nr1 && ok==1)
{
fprintf(pg,"%d %d %d",v[i],v[j],v[k]);
ok=0;
}
ok=1;
for (i=1; i<=n; i++)
for (j=1; j<=n; j++)
for (k=1; k<=n; k++)
if (v[i]+v[j]+v[k]==nr2 && ok==1)
{fprintf(pg," %d %d %d\n",v[i],v[j],v[k]);
ok=0;
}
if (ok==1) fprintf(pg,"-1");
return 0;
}