Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <algorithm>
- using namespace std;
- const int mod=1000*1000*1000;
- class fraction
- {
- protected:
- int fir, sec;
- public:
- void init (int a=0, int b=0)
- {
- fir=a; sec=b;
- }
- friend istream& operator>> (istream& stream, fraction& d){
- char str[50]; int a=0, b=0, znak=1;
- stream >> str; int i=0;
- if (str[0]=='-') {znak*=(-1); i++;}
- while (str[i]!='.') {
- a=a*10+(int)str[i]-'0';
- i++;
- }
- a*=znak;
- i++;
- while (str[i]) {
- b=b*10+(int)str[i]-'0'; i++;
- }
- while (b<mod/10) {
- b*=10;
- }
- if (a<0) {a--; b=mod-b;}
- d.init (a, b);
- return stream;
- }
- friend ostream& operator<< (ostream& stream, const fraction& d) {
- int sec = d.sec;
- int fir = d.fir;
- if (fir<0 && sec!=0) {
- fir++;
- sec = mod-sec;
- }
- while (sec%10==0) sec/=10;
- stream << fir << "." << sec;
- return stream;
- }
- fraction (int a, int b){init (a,b);}
- fraction () {}
- fraction (const fraction & obj):fir(obj.fir), sec(obj.sec){}
- fraction & operator = (const fraction & q)
- {
- fir=q.fir; sec=q.sec; return *this;
- }
- fraction operator+ (const fraction & oth)
- {
- int a, b;
- b=(sec+oth.sec)%mod;
- while (b<mod/10) {
- b*=10;
- }
- a=fir+oth.fir+(sec+oth.sec)/mod;
- fraction z(a, b);
- return z;
- }
- fraction operator * (const fraction & oth) {
- // ([x] + {x})*([y] + {y})
- // [x]*[y] + [x]*{y} + [y]*{x} + {x}*{y}
- int resf = 0, ress = 0;
- resf += fir * oth.fir;
- long long prom = (long long)fir*oth.sec;
- prom += (long long)oth.fir*sec;
- resf += prom/mod;
- ress += prom%mod;
- prom = (long long)sec * oth.sec;
- ress += prom/mod;
- return fraction(resf+ress/mod, ress%mod);
- }
- fraction operator - (const fraction &oth)
- {
- int a, b;
- int oth_fir=oth.fir; int oth_sec=oth.sec;
- if (oth_fir<0) {oth_fir++; oth_fir*=(-1);oth_sec=mod-oth_sec;}
- else {oth_fir*=(-1); oth_fir--; oth_sec=mod-oth_sec;}
- b=(sec+oth_sec)%mod;
- while (b<mod/10) {
- b*=10;
- }
- a=fir+oth_fir+(sec+oth_sec)/mod;
- fraction z(a, b);
- return z;
- }
- fraction & operator += (const fraction & oth)
- {
- fir += oth.fir;
- sec += oth.sec;
- return *this;
- }
- bool operator < (const fraction & oth) const
- {
- if (fir < oth.fir) return true;
- else if (fir>oth.fir) return false;
- else if (fir==oth.fir)
- {
- if (sec<oth.sec) return true;
- else return false;
- }
- }
- bool operator <= (const fraction & oth) const
- {
- if (this < oth || (fir==oth.fir && sec==oth.sec) ) return true;
- return false;
- }
- };
- void RunTests() {
- // Test #1
- }
- int main ()
- {
- RunTests();
- fraction a[200];
- int n;
- cin >> n;
- for (int i=0;i<n;i++) cin >> a[i];
- sort(a, a+n);
- for (int i=0;i<n;i++) cout << a[i] << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment