Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using std::cin;
- using std::cout;
- using std::vector;
- struct CPoint {
- int X;
- int Y;
- CPoint( int x, int y ) : X( x ), Y( y ) {}
- };
- // Вариант 1.
- // Оператор сравнения по удаленности точки от (0, 0).
- bool operator<( const CPoint& first, const CPoint& second )
- {
- return first.X * first.X + first.Y * first.Y <
- second.X * second.X + second.Y * second.Y;
- }
- bool operator<=( const CPoint& first, const CPoint& second )
- {
- return !( second < first );
- }
- bool operator>( const CPoint& first, const CPoint& second )
- {
- return second < first;
- }
- bool operator>=( const CPoint& first, const CPoint& second )
- {
- return !( first < second );
- }
- // Вариант 2. Функция сравнения.
- bool CompareByDistanceFromZero( const CPoint& first, const CPoint& second )
- {
- return first.X * first.X + first.Y * first.Y <
- second.X * second.X + second.Y * second.Y;
- }
- // Сортировка пузырьком.
- template<class T>
- void BubbleSort( vector<T>& arr, bool (* compareFunction)( const T&, const T& ) )
- {
- int n = arr.size();
- for( int i = n - 2; i >= 0; --i ) { // n - 1 итерация.
- for( int j = 0; j <= i; ++j ) {
- if( compareFunction( arr[j + 1], arr[j] ) ) {
- std::swap( arr[j], arr[j + 1] );
- }
- }
- }
- }
- int main()
- {
- vector<CPoint> arr;
- int x = 0;
- int y = 0;
- while( cin >> x >> y ) {
- arr.push_back( CPoint( x, y ) );
- }
- BubbleSort( arr, CompareByDistanceFromZero );
- for( int i = 0; i < static_cast<int>( arr.size() ); ++i ) {
- cout << arr[i].X << ' ' << arr[i].Y << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement