Advertisement
catalyn

fractii

Apr 1st, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5. /* se citesc n fractii dintr-un fisier. afisati fractiile reductibile ordonate
  6. crescator.
  7. ex:
  8. n=4
  9. 3,6 2,8 3,7 2,20
  10. se vor afisa:
  11. 2/20 2/8 3/6
  12. */
  13. #include <fstream>
  14.  
  15. using namespace std;
  16.  
  17. ifstream f("date.in");
  18. ofstream g("date.out");
  19.  
  20. struct fractie
  21. {
  22. int a1,a2;
  23. };
  24.  
  25. fractie v[1000],nou[1000];
  26.  
  27. int cmmdc(int a,int b)
  28. {
  29. int r;
  30. r=a%b;
  31. while(r)
  32. {
  33. a=b;
  34. b=r;
  35. r=a%b;
  36. }
  37. return b;
  38. }
  39.  
  40. int main()
  41. {
  42. int n,i,k=0,x,y,j;
  43. f>>n;
  44. for(i=1;i<=n;i++)
  45. {
  46. f>>x>>y;
  47. if(cmmdc(x,y)!=1)
  48. v[++k].a1=x,v[k].a2=y;
  49. }
  50. for(i=1;i<=k-1;i++)
  51. for(j=i+1;j<=k;j++)
  52. if((v[i].a1*v[j].a2)>(v[j].a1*v[i].a2))
  53. swap(v[i],v[j]);
  54. for(i=1;i<=k;i++)
  55. g<<v[i].a1<<"/"<<v[i].a2<<'\n';
  56. return 0;
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement