Pagini recente » Cod sursa (job #1854021) | Cod sursa (job #239011) | Cod sursa (job #2539310) | Cod sursa (job #1690523) | Cod sursa (job #3135507)
#include <stdio.h>
#include <stdlib.h>
#define inpath "euclid3.in"
#define outpath "euclid3.out"
int euclid(int a,int b,int *x,int *y){
int x0,y0,d;
if(!b)
{
*x = 1;
*y = 0;
return a;
}
else
{
d = euclid(b, a % b, &x0, &y0);
*x = y0;
*y = x0 - (a / b) * y0;
}
return d;
}
int main(){
FILE *in = NULL, *out = NULL;
if( (in = fopen(inpath, "r")) == NULL )
{
printf("Eroare la deschiderea fisierului %s.\n", inpath);
exit(EXIT_FAILURE);
}
if( (out = fopen(outpath, "w")) == NULL )
{
printf("Eroare la deschiderea fisierului %s.\n", outpath);
exit(EXIT_FAILURE);
}
int i,a,b,c,d,x,y,t;
if(fscanf(in,"%d",&t) != 1)
{
printf("Eroare citire t \n");
exit(EXIT_FAILURE);
}
for(i = 0; i < t; ++i)
{
if(fscanf(in,"%d%d%d",&a,&b,&c) != 3)
{
printf("Eroare citire t \n");
exit(EXIT_FAILURE);
}
d = euclid(a, b, &x, &y);
if(c % d)
{
fprintf(out,"0 0\n");
}
else
{
fprintf(out,"%d %d\n",x * (c / d), y * (c / d));
}
}
if( fclose(in) != 0 )
{
printf("Eroare la inchiderea fisierului %s.\n", inpath);
exit(EXIT_FAILURE);
}
if( fclose(out) != 0 )
{
printf("Eroare la inchiderea fisierului %s.\n", outpath);
exit(EXIT_FAILURE);
}
return 0;
}