Advertisement
omar-alhelo

Untitled

Jul 16th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.45 KB | None | 0 0
  1. /// __Macro's__
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define ll                   long long
  5. #define II                   pair<int,int>
  6. #define III                  pair<int, II>
  7. #define VL                   vector<ll>
  8. #define VI                   vector<int>
  9. #define VII                  vector<II>
  10. #define VIII                 vector<III>
  11. #define VVI                  vector<vector<int>>
  12. #define VVII                 vector<vector<II>>
  13. #define fr                   first
  14. #define sc                   second
  15. #define mkpr                 make_pair
  16. #define PQ                   priority_queue
  17. #define pb                   push_back
  18. #define eb                   emplace_back
  19. #define all(v)               v.begin(), v.end()
  20. #define LINE                 putc('\n', stdout)
  21. #define SPACE                putc(' ' , stdout)
  22. #define TAB                  putc('\t', stdout)
  23. #define msg(x)               printf(#x)
  24. #define fillarr(arr, val)    fill((int*)arr, (int*)arr+(sizeof(arr)/sizeof(int)), val)
  25. #define _log2(x)             (31-__builtin_clz(x))
  26. #define INF                  (ll)2e17
  27. #define MOD                  1000000007
  28. #define getint ReadInt()
  29. const char IO_MODE = 0; // in-->00<--out (MSB-->LSB) | 0: Norm, 1: Fast
  30. constexpr ll operator "" _K(unsigned ll n) {return 1000*n;}
  31. constexpr ll operator "" _M(unsigned ll n) {return 1000000*n;}
  32. void operator "" _print(char const* s, size_t n) {printf("%s\n", s);}
  33. inline ll ReadInt(){ll x=0,s=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')s=-1;c=getchar();}while(c>='0'&&c<='9'){x=x*10+c-'0';
  34. c=getchar();}return s*x;}inline void WriteInt(ll x){char c[20];if(!x){putchar('0');return;}if(x<0)putchar('-'),x=-x;int i=0;while(x>0)
  35. c[++i]=x%10,x/=10;while(i)putchar(c[i--]+48);}template <typename T>inline void out(T x){if(IO_MODE&1)WriteInt(x);else if(typeid(x)==typeid(int))
  36. printf("%i", x);else if(typeid(x)==typeid(ll)) printf("%lld", x);}template<typename T, typename... Args>inline void out(T x,Args...args)
  37. {out(x);SPACE;out(args...);}template <typename T>inline void in(T &x){if(IO_MODE&2) x=ReadInt();else if(typeid(x)==typeid(int))
  38. scanf("%i",&x);else if(typeid(x)==typeid(ll)) scanf("%lld",&x);}template<typename T, typename... Args>
  39. inline void in(T &x,Args&...args){in(x);in(args...);}
  40. int aa,bb,cc,dd,ee,ff,gg,hh,ii,jj,kk,mm,nn,oo,pp,qq,rr,ss,tt,uu,vv,ww,xx,yy,zz;
  41. int tc;
  42. ///
  43.  
  44. #define double long double
  45.  
  46. int main()
  47. {
  48.   in(tc);
  49.   rep:
  50.   while(tc--)
  51.   {
  52.     double d;
  53.     int prec;
  54.     string s;
  55.     cin>>s;
  56.  
  57.     stringstream ss(s);
  58.     ss>>d;
  59.  
  60.     for(int i=0 ; i<s.size() ; i++)
  61.       if(s[i]=='.')
  62.         prec=s.size()-i-1;
  63.  
  64.     double real = floor(d);
  65.     double decimal = d-real;
  66.  
  67.     if((ll)real)
  68.     {
  69.       if(abs(decimal)<1e-20) {cout << (ll)real << "\"" << endl; goto rep;}
  70.       else  cout << (ll)real << " ";
  71.     }
  72.     d = decimal;
  73.  
  74.     double p=1.0;
  75.     while(prec--) p*=10;
  76.  
  77.     double b=1.0;
  78.     for(int i=0 ; i<=128 ; i++)
  79.     {
  80.  
  81.       double a = b*d;
  82.  
  83.       double a1 = floor(a);
  84.       double a2 = ceil(a);
  85.      
  86.       double d1 = a1/b;
  87.       double d2 = a2/b;
  88.  
  89.       if( abs(d-round(d1*p)/p)<1e-16 || abs(d-ceil(d1*p)/p)<1e-16 || abs(d-floor(d1*p)/p)<1e-16 )
  90.           {cout << round(a1) << "/"<<round(b)<<"\"" << endl; goto rep;}
  91.        
  92.       if( abs(d-round(d2*p)/p)<1e-16 || abs(d-ceil(d2*p)/p)<1e-16 || abs(d-floor(d2*p)/p)<1e-16 )
  93.           {cout << round(a2) << "/"<<round(b)<<"\"" << endl; goto rep;}
  94.  
  95.       b*=2;
  96.     }
  97.   }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement