fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int MAX_LENGTH = 100;
  5. const int MAX_VALUE = 1000000000;
  6.  
  7. int main() {
  8. int n, x, v[MAX_LENGTH + 1];
  9. cin >> n >> x;
  10. for (int i = 1; i <= n; ++i) {
  11. cin >> v[i];
  12. }
  13. int closeDiference = MAX_VALUE, closePos = MAX_LENGTH;
  14. for (int i = 1; i <= n; ++i) {
  15. int diference = 0, position;
  16. if (x > v[i]) {
  17. diference = x - v[i];
  18. position = i;
  19. } else {
  20. diference = v[i] - x;
  21. position = i;
  22. }
  23. if (diference < closeDiference ) {
  24. closeDiference = diference;
  25. closePos = position;
  26. }
  27. }
  28. cout << closePos;
  29. return 0;
  30. }
Success #stdin #stdout 0.01s 5304KB
stdin
5 100
0 0 100 0 0 
stdout
3