F
F
Faha19982016-04-06 15:08:22
Arrays
Faha1998, 2016-04-06 15:08:22

Iterate over an array (js), how?

The question may seem stupid, in some places it may be stupid, but I hope for your kindness), don't kick me hard))
I'm trying to solve this problem codeforces.com/contest/158/problem/A
I think it can be solved by enumerating the array

var i = prompt("Введите ваши числа(n и k)").split(" ");
var n = i[0];
var k = i[1];
var p = prompt("Введите очки участников").split(" ");
var d = k;
  for(b = p[k--];b <= n; b++){
    for(h = p[k]; h<= n; h++){
      if(b == h){
        d++;
      }
    }
  }
console.log(d);

I enter data
4 3
1 1 1 1
k = 3
b = unit number 3
h = unit number 4
d = 3
because the loop should check if b = h, then d++
why does it output that d = 7??, because the limit is up to 4, where do 3 more runs come from?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Faha1998, 2016-04-06
@Faha1998

var i = prompt("Введите ваши числа(n и k)").split(" ");
var n = i[0];
var k = i[1];
var p = prompt("Введите очки участников").split(" ");
var d = k;
  for(b = p[k- 1];k<p.length; k++){
    var h = p[k];
    if(b == h){
      d++;
    }
  }
console.log(d);

Initially, the concept of the increment was incorrect, it was necessary to increase k so that b would move along the array, and not the number itself.
Everything worked

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question