fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int gcd(m,n){
  5.  
  6. if (n==0){
  7. return m;
  8. }
  9. else{
  10. return gcd(n,m%n);
  11. }
  12. }
  13.  
  14. void process(int y1,int m1,int b1,int y2,int m2,int b2,double *smx, double *bgx, int ind, int *smallind, int *bigind, double *savex, double *savey){
  15. int div,lcm,first,second,temp1,temp;
  16. double x,y;
  17. div=abs(gcd(y1,y2));
  18. lcm=(y1*y2)/div;
  19. first=lcm/y1;
  20. second=lcm/y2;
  21.  
  22. m1=m1*first;
  23. b1=b1*first;
  24. y1=y1*first;
  25. m2=m2*second;
  26. b2=b2*second;
  27. y2=y2*second;
  28.  
  29. if(y1>0&&y2>0){
  30. temp1=m1-m2;
  31. temp=b2-b1;
  32. x=((float)temp)/((float)temp1);
  33. y=(((float)m1*x)+((float)b1))/((float)lcm);
  34. }
  35. else if(y1!=y2){
  36. temp1=m1+m2;
  37. temp=b2+b1;
  38. x=((float)temp)/((float)temp1);
  39. y=0-(((float)m1*x)+((float)b1))/((float)lcm);
  40. }
  41. if(x<*smx){
  42. *smx=x;
  43. *smallind=ind;
  44. }
  45. if(x>*bgx){
  46. *bgx=x;
  47. *bigind=ind;
  48. }
  49. *savex=x;
  50. *savey=y;
  51. }
  52.  
  53. int main(){
  54. int type,testcase,x1,y1,x2,y2,numm,denm,numb,denb,div,div2;
  55. int save[3][3];
  56. scanf("%d",&type);
  57. if(type==1){
  58. scanf("%d",&testcase);
  59.  
  60. for(int a=0;a<testcase;a++){
  61. scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
  62. numm=y1-y2;
  63. denm=x1-x2;
  64. if(numm<0 && denm<0){
  65. numm=abs(numm);
  66. denm=abs(denm);
  67. }
  68. numb=((x2*y1)-(x1*y2));
  69. denb=x2-x1;
  70. if(numb<0 && denb<0){
  71. numb=abs(numb);
  72. denb=abs(denb);
  73. }
  74. div=gcd(numm,denm);
  75. numm=numm/div;
  76. denm=denm/div;
  77. div2=gcd(numb,denb);
  78. numb=numb/div;
  79. denb=denb/div;
  80. if(denm<0&&numm>0){
  81. numm=0-numm;
  82. denm=abs(denm);
  83. }
  84. if(denb<0&&numb>0){
  85. numb=0-numb;
  86. denb=abs(denb);
  87. }
  88.  
  89. printf("y = ");
  90. if((numm!=0)&&(numm!=-1)&&(numm!=1)&&(denm==1||denm==-1)) {
  91. numm=numm*denm;
  92. printf("%dx",numm);
  93. }
  94. else if(numm==1&&denm==1){
  95. printf("x");
  96. }
  97. else if(numm==-1&&denm==1){
  98. printf("-x");
  99. }
  100. else if((denm<0&&numm>0)||(denm>0&&numm<0)){
  101. denm=abs(denm);
  102. printf("%d/%dx",numm,denm);
  103. }
  104. else if(denm>0&&numm>0){
  105. printf("%d/%dx",abs(numm),abs(denm));
  106. }
  107.  
  108. if(numb!=0&&numm!=0){
  109. if((numb<0)){
  110. printf(" - ");
  111. }
  112. else{
  113. printf(" + ");
  114. }
  115. }
  116. if((numb!=0)&&(denb==1)){
  117. numb=numb*denb;
  118. printf("%d",abs(numb));
  119. }
  120. else if(numb!=0){
  121. printf("%d/%d",abs(numb),abs(denb));
  122. }
  123. printf("\n");
  124. }
  125.  
  126.  
  127.  
  128. }
  129. /*
  130.   for(int i=0;i<testcase;i++){
  131.   printf("%d %d %d\n",save[i][0],save[i][1],save[i][2]);
  132.   }
  133.   */
  134. double ansx,ansy,smallx=100.00,bigx=-100.00,ans[3][3],savex,savey;
  135. if(type==2){
  136. for(int i=0;i<3;i++){
  137. scanf("%d %d %d",&save[i][0],&save[i][1],&save[i][2]);
  138. }
  139.  
  140. int ind=0,smallind,bigind;
  141. process(save[0][0],save[0][1],save[0][2],save[1][0],save[1][1],save[1][2],&smallx,&bigx,ind,&smallind,&bigind,&savex,&savey);
  142. ans[ind][0]=savex;
  143. ans[ind][1]=savey;
  144. ind++;
  145. process(save[0][0],save[0][1],save[0][2],save[2][0],save[2][1],save[2][2],&smallx,&bigx,ind,&smallind,&bigind,&savex,&savey);
  146. ans[ind][0]=savex;
  147. ans[ind][1]=savey;
  148. ind++;
  149. process(save[1][0],save[1][1],save[1][2],save[2][0],save[2][1],save[2][2],&smallx,&bigx,ind,&smallind,&bigind,&savex,&savey);
  150. ans[ind][0]=savex;
  151. ans[ind][1]=savey;
  152. int middle=3-smallind-bigind;
  153.  
  154. printf("(%.2f, %.2f)\n",ans[smallind][0],ans[smallind][1]);
  155. printf("(%.2f, %.2f)\n",ans[middle][0],ans[middle][1]);
  156. printf("(%.2f, %.2f)\n",ans[bigind][0],ans[bigind][1]);
  157.  
  158. }
  159.  
  160.  
  161. return 0;
  162. }
Success #stdin #stdout 0s 5460KB
stdin
1
5
2 -2 7 2
1 3 5 -3
5 2 -3 -2
3 1 -1 4
5 -10 -3 -10
stdout
y = 4/5x - 18/5
y = -3/2x + 9/2
y = 1/2x - 1/2
y = -3/4x + 13/4
y = 10