Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
    }
}

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading