Advertisement
icatalin

21.02.2017 problema fractii

Feb 21st, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. /*
  2. Se citesc n fractii dintr-un fisier text. Afisati doar fractiile care sunt reductibile si ordonati-le crescastor.
  3. Ex:
  4. IN:  3/6 2/8 3/7 2/20
  5. OUT:  2/20 2/8 3/7
  6. */
  7.  
  8. #include <iostream>
  9. #include <fstream>
  10.  
  11. using namespace std;
  12.  
  13. ifstream ff("date2.in");
  14.  
  15. struct fractie
  16. {
  17.     float x,y; // x = sus y - jos
  18.     float nr;
  19. }v[100];
  20.  
  21. int cmmdc(int a, int b)
  22. {
  23.     int r;
  24.     r=a%b;
  25.     while (r)
  26.     {
  27.         a=b;
  28.         b=r;
  29.         r=a%b;
  30.     }
  31.  
  32.     return b;
  33. }
  34.  
  35. int main()
  36. {
  37.     int n;
  38.     cin>>n;
  39.  
  40.     for (int i=1; i<=n ;i++)
  41.     {
  42.         ff>>v[i].x>>v[i].y;
  43.  
  44.         v[i].nr=v[i].x/v[i].y;
  45.     }
  46.        
  47.  
  48.     for (int i=1; i<n; i++)
  49.         for (int j=i+1; j<=n; j++)
  50.         if (v[i].nr > v[j].nr)
  51.             swap(v[i],v[j]);
  52.  
  53.  
  54.     for (int i=1; i<=n; i++)
  55.         if (cmmdc(v[i].x,v[i].y)!=1)
  56.             cout<<v[i].x<<"/"<<v[i].y<<'\n';
  57.  
  58.  return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement