Cod sursa(job #253066)

Utilizator Data 5 februarie 2009 13:30:48 Ecuatie 10 c done Arhiva de probleme 1.56 kb
``````#include <stdio.h>
#include <math.h>

FILE *f,*g;
long A,B,C,p1,p2,q1,q2,k,ok;

void genereaza(void)
{
if(q1==abs(C))
{
q1=0-abs(C);
q2=C/q1;
do
{
p1++;
if(p1==0)p1++;
}
while(A%p1!=0);
p2=A/p1;
}
else
{
do
{
q1++;
if(q1==0)q1++;
}
while(C%q1!=0);
q2=C/q1;
}
}

void afisare(void)
{
if((k!=0)&&((abs(A)==p1)&&(q1==abs(C))))
{
fprintf(g,"-1\n");
}
else
{
//afisez prima parte
fprintf(g,"(");
if(p1==1)
fprintf(g,"x");
else
if(p1==-1)
fprintf(g,"-x");
else
fprintf(g,"%ldx",p1);
if(q1<0)
fprintf(g,"%ld)",q1);
else
fprintf(g,"+%ld)",q1);

//afisez a doua parte
fprintf(g,"(");
if(p2==1)
fprintf(g,"x");
else
if(p2==-1)
fprintf(g,"-x");
else
fprintf(g,"%ldx",p2);
if(q2<0)
fprintf(g,"%ld)",q2);
else
fprintf(g,"+%ld)",q2);
fprintf(g,"\n");
}
}

int main()
{
f=fopen("ecuatie.in","r");
g=fopen("ecuatie.out","w");
fscanf(f,"%ld %ld %ld %ld\n",&A,&B,&C,&k);
/*
P1 * P2 = A
P1 * Q2 + P2 * Q1 = B
Q1 * Q2 = C
*/
// initializez p1 si p2
p1=0-abs(A);
p2=A/p1;
//initializez q1 si q2
q1=0-abs(C);
q2=C/q1;
if((p1*q2+p2*q1)==B)
{
k--;
}
while(((abs(A)!=p1)||(q1!=abs(C)))&&k!=0)
{
genereaza();
if((p1*q2+p2*q1)==B)
{
k--;
}
}
afisare();
fclose(f);   fclose(g);
return 0;
}
``````