fork download
  1. #include <cmath>
  2. #include <vector>
  3. #include <iostream>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. int main() {
  8. vector <int> a, b;
  9. int element;
  10. while (cin >> element) {
  11. a.push_back(element);
  12. }
  13. bool getNegative = true;
  14. for (int i = 0; i < a.size() - 1; i++) {
  15. if (abs(a[i]) % 2 == 1 && a[i + 1] % 2 == 0) {
  16. getNegative = false;
  17. break;
  18. }
  19. }
  20. if (getNegative) {
  21. for (int i = a.size() - 1; i >= 0; i--) {
  22. if (a[i] < 0) {
  23. b.push_back(a[i]);
  24. }
  25. }
  26. } else {
  27. for (int i = a.size() - 1; i >= 0; i--) {
  28. if (a[i] > 0) {
  29. b.push_back(a[i]);
  30. }
  31. }
  32. }
  33. for (int i = 0; i < b.size(); i++) {
  34. cout << b[i] << " ";
  35. }
  36. return 0;
  37. }
Success #stdin #stdout 0s 3460KB
stdin
-1 -4 5 7
stdout
7 5