fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n;
  6. cin >> n;
  7. int *x = new int [n];
  8. for ( int i = 0; i < n; i++ ) cin >> x [i];
  9. int min = 100;
  10. int max = -100;
  11. for ( int i = 0; i < n; i++ ) {
  12. if ( x [i] > max ) max = x [i];
  13. if ( x [i] < min ) min = x [i];
  14. }
  15. for ( int i = 0; i < n; i++ ) {
  16. if ( x [i] == max ) x [i] = min;
  17. else if ( x [i] == min ) x [i] = max;
  18. }
  19. for ( int i = 0; i < n; i++ ) cout << x [i] << " ";
  20. return 0;
  21. }
Success #stdin #stdout 0s 4528KB
stdin
7
3 5 -7 7 5 -9 -4
stdout
3 5 -7 -9 5 7 -4