Help me in solving LBC17 problem

My issue

I am getting the wrong answer for this solution. Where I am doing wrong?

My code

// Update the code below to solve this problem

#include <iostream>
#include <string>
#include <cmath>
using namespace std;

int main() 
{
  int t;
  cin>>t; 
  while(t--)
  {
  int X, Y;
  cin>>X>>Y;
  if(X>0&& X<=10){
    int x = Y-10;
    cout<<abs(x/10)<<endl;
  }
  else if (X>10 && X<=20){
    int x = Y-20;
    cout<<abs(x/10)<<endl;
  }
  else if (X>20 && X<=30){
    int x=Y-30;
    cout<<abs(x/10)<<endl;
  }
  else if (X>30 && X<=40){
    int x=Y-40;
    cout<<abs(x/10)<<endl;
  }
  else if (X>40 && X<=50){
    int x = Y-50;
    cout<<abs(x/10)<<endl;
  }
  else if (X>50 && X<=60){
    int x=Y-60;
    cout<<abs(x/10)<<endl;
  }
  else if (X>60 && X<=70){
    int x=Y-70;
    cout<<abs(x/10)<<endl;
  }
  else if (X>70 && X<=80){
    int x=Y-80;
    cout<<abs(x/10)<<endl;
  }
  else if (X>80 && X<=90){
    int x=Y-90;
    cout<<abs(x/10)<<endl;
  }
  else if (X>90 && X<=100){
    int x=Y-100;
    cout<<abs(x/10)<<endl;
  }
   }
return 0;
}

Learning course: Solve Programming problems using C++
Problem Link: CodeChef: Practical coding for everyone

You have too many if else statements making it a bit confusing, maybe try this:
int fx = (X-1)/10 + 1; // floor number of x
int fy = (Y-1)/10 + 1; // floor number of y
cout<<abs(fx-fy)<<endl; // number of floor x need to cross to reach y

1 Like

@mkandan1
you have too many if else ladder condition so its tough to check which corner test case you are missing . I have used a easy approach which u can understand.

using the condition given in the question i found the range for room numbers in each floor.
and then found out the floor in which chef is as well as chefina.
and then printed the difference between their floors.

I have pasted my code below
Hope this helps!!

#include <iostream>
#include <string>
using namespace std;

int main() 
{
  int t;
  cin>>t; 
  while(t--)
  {
  int x, y;
  cin>>x>>y;
  int ans,ans1=0;
  for(int i=1;i<=10;i++)
  {
      int k=10*(i-1)+10;
      int z=k-9;
      if(z<=x&&x<=k)//finding in which floor their room is .
      {
        ans =i;
      }
      if(z<=y&&y<=k)
      {
         ans1=i; 
      }

  }
  cout<<abs(ans-ans1)<<endl;
 
   }
return 0;
}

Thank you @anish1301 for your detailed answer. It’s works