Sortowanie liczb zgodnie z wolą użytkownika
Ostatnio zmodyfikowano 2016-02-23 14:28
carlosmay |
» 2016-02-23 14:28:20 Prosty przykład z użyciem std::vector. #include <iostream> #include <vector>
void sortuj( std::vector < int >& v, bool mode = false ) { for( int i = 0; i < v.size(); i++ ) { for( int j = 0; j < v.size() - 1; j++ ) { if( !mode ) { if( v[ j ] < v[ j + 1 ] ) std::swap( v[ j ], v[ j + 1 ] ); } else { if( v[ j + 1 ] < v[ j ] ) std::swap( v[ j ], v[ j + 1 ] ); } } } }
int main() { using std::cout; using std::endl; std::vector < int > v = { 1, 3, 5, 6, 2, 8, 7 }; for( int el: v ) cout << el << ' '; cout << endl; sortuj( v ); for( int el: v ) cout << el << ' '; cout << endl; sortuj( v, true ); for( int el: v ) cout << el << ' '; cout << endl; return 0; } |
|
1 « 2 » |