Cod sursa(job #333516)

Utilizator iulia609fara nume iulia609 Data 23 iulie 2009 00:34:38
Problema Ecuatie Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.82 kb
#include<stdio.h>
#include<math.h>
#define dim 100000001
using namespace std;

int p1[dim],p2[dim],q1[dim],q2[dim];

void qsort(int in,int sf)   
    {long i,j;   
     long temp,aux;   
     i=in,j=sf;   
     temp=p1[(i+j)>>1];   
        
     do  
       {while(p1[i]<temp)i++;   
        while(p1[j]>temp)j--;   
        if(i<j) {aux=p1[i],p1[i]=p1[j],p1[j]=aux;  
				 aux=p2[i],p2[i]=p2[j],p2[j]=aux;  
				 aux=q1[i],q1[i]=q1[j],q1[j]=aux;  
				 aux=q2[i],q2[i]=q2[j],q2[j]=aux;}  
        if(i<=j)j--,i++;   
        } while(i<=j);   
     if(in<j)qsort(in,j);   
     if(i<sf)qsort(i,sf);   
     }   

void qsort1(int in,int sf)   
    {long i,j;   
     long temp,aux;   
     i=in,j=sf;   
     temp=q1[(i+j)>>1];   
        
     do  
       {while(q1[i]<temp)i++;   
        while(q1[j]>temp)j--;   
        if(i<j) {aux=p1[i],p1[i]=p1[j],p1[j]=aux;  
				 aux=p2[i],p2[i]=p2[j],p2[j]=aux;  
				 aux=q1[i],q1[i]=q1[j],q1[j]=aux;  
				 aux=q2[i],q2[i]=q2[j],q2[j]=aux;}  
        if(i<=j)j--,i++;   
        } while(i<=j);   
     if(in<j)qsort(in,j);   
     if(i<sf)qsort(i,sf);   
     }   


int main()
{  int a,b,c,k,D,cont,x1,x2,i,c1,c2,j,aux,x;
	
	FILE *f = fopen("ecuatie.in", "r");
	FILE *g = fopen("ecuatie.out", "w");
	
	fscanf(f, "%d%d%d%d", &a, &b, &c, &k);
	
	cont = 0;
	for(i = 1; i <= a; i++)
		{
			if(a%i == 0) 
				{
					cont++;
					p1[cont] = i, p2[cont] = a/i;
					D = b*b - 4*a*c;
					x1 = (b*-1 + sqrt(D))/(2*a);
					x2 = (b*-1 - sqrt(D))/(2*a);
					q1[cont] = x1*-1*i;
					q2[cont] = x2*-1*(a/i);
					 
					cont++;
					p1[cont] = -1*p1[cont-1];
					p2[cont] = -1*p2[cont-1];
					q2[cont] = -1*q2[cont-1];
					q1[cont] = -1*q1[cont-1];
					
					cont++;
					p1[cont] = -1*p1[cont-1];
					p2[cont] = -1*p2[cont-1];
					q2[cont] = x1*-1*(a/i);
					q1[cont] = x2*-1*i;
					
					cont++;
					p1[cont] = -1*p1[cont-1];
					p2[cont] = -1*p2[cont-1];
					q2[cont] = -1*q2[cont-1];
					q1[cont] = -1*q1[cont-1];
				}	
		}
	
	qsort(1, cont);
	
	i = 1;
	while(i <= cont)
		{
			c2 = 1; j = i;
			while(p1[j] == p1[j+1]) c2++,j++;
			if(c2 == 2) {aux=p1[i],p1[i]=p1[j],p1[j]=aux;  
						 aux=p2[i],p2[i]=p2[j],p2[j]=aux;  
						 aux=q1[i],q1[i]=q1[j],q1[j]=aux;  
						 aux=q2[i],q2[i]=q2[j],q2[j]=aux;} 
			else if(c2 != 1) qsort1(i, i+c2-1);
			i++;
		}
	
/*	c1 = 0;
	for(i = 1; i <= cont; i++)
		{if(p1[i] == p1[i+1] && p2[i] == p2[i+1] && q1[i] == q1[i+1] && q2[i] == q2[i+1]);
			else c1++;
			if(c1 == k) break;
		}
	*/ c1 = k; 
	
	fprintf(g, "(%dx",p2[c1]);
	if(q2[c1] > 0) fprintf(g, "+%d)(",q2[c1]);
		else {x = -1*q2[c1]; fprintf(g, "-%d)(",x);}
	fprintf(g, "%dx",p1[c1]);
	if(q1[c1] > 0) fprintf(g, "+%d)\n",q1[c1]);
		else {x = -1*q1[c1]; fprintf(g, "-%d)\n",x);}
	
	fclose(f);
	fclose(g);
	return 0;
}