Home > Net >  Why my code is not working for problem (Marathon) Code forces
Why my code is not working for problem (Marathon) Code forces

Time:07-05

You are given four distinct integers a, b, c, d.

Timur and three other people are running a marathon. The value a is the distance that Timur has run and b, c, d correspond to the distances the other three participants ran.

Output the number of participants in front of Timur.

Input The first line contains a single integer t (1≤t≤104) — the number of test cases.

The description of each test case consists of four distinct integers a, b, c, d (0≤a,b,c,d≤104).

Output For each test case, output a single integer — the number of participants in front of Timur.

#include <iostream>

using namespace std;

int main()
{
    int t, p(0);
    cin >> t;
    while (t--) {
        int a, b, c, d, x;
        cin >> a >> b >> c >> d;
        if (b > a) {
            p  ;
        } else if (c > a) {
            p  ;
        } else if (d > a) {
            p  ;
        }
        cout << p << endl;
    }
}

CodePudding user response:

Try changing the else ifs to ifs and declaring the variable p inside the loop:

#include <iostream>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int p = 0;
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if (b > a) {
      p  ;
    }
    if (c > a) {
      p  ;
    }
    if (d > a) {
      p  ;
    }
    cout << p << '\n';
  }
}

Example Usage:

4
2 3 4 1
10000 0 1 2
500 600 400 300
0 9999 10000 9998
2
0
1
3

CodePudding user response:

When using "else if" you only check the "else if" if the if wasn't true, so in this case if b > a is true then the others won't be checked, to check individually you should just use if each time instead of else if

  • Related