Playground
#include <iostream> #include <vector> #include <algorithm> using namespace std; struct Frac { int a, b; bool operator <(const Frac &o) const { //treba da se sporedi (a/b) so (o.a/o.b) //a/b < o.a/o.b, ako a*(o.b) < b*(o.a) //objasnuvanje: pomnozhi gi dvete strani so (b)*(o.b) return (a*o.b < b*o.a); } }; int main() { Frac f1; f1.a = 1; f1.b = 2; Frac f2; f2.a = 1; f2.b = 3; Frac f3; f3.a = 3; f3.b = 4; vector<Frac> v; v.push_back(f1); //v={1/2}; v.push_back(f2); //v={1/2, 1/3}; v.push_back(f3); //v={1/2, 1/3, 3/4}; sort(v.begin(), v.end()); //v={1/3, 1/2, 3/4); for (int i=0; i< (int) v.size(); i++) cout << v[i].a << "/" << v[i].b << " "; //pechati '1/3 1/2 3/4 ' return 0; }
Input data
Program output
no input
Execute
Language: ????????? | Memory: ???? KB | Time: ??? ms
Nothing has been executed, yet!