Help me in solving LBC21 problem

My issue

While submitting, the code is getting "wrong answer". I cannot figure it out why!!  

My code


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

int main() 
{
  int t;
  cin>>t; 
  while(t--)
  {
  int P;
  cin>>P;
  if(P>1000) cout<<-1<<endl; //check if P is greater than 1000, which is invalid.
  else if(P==0) cout<<0<<endl; //check if P is 0, no question is answered correctly.
  else {
    if(P>=100) {
        if(P%100>(10-(P/100))) cout<<-1<<endl; //if YES, input is invalid, because there are only 10 questions 
        else if(P%100 == 0) cout<<P/100<<endl; //if all the answered questions are carrying 100 marks  each
        else cout<<P%100+P/100<<endl; //total answered questions, quotient is the number of 100 marked & remainder is the number of 1 marked question answered. 
    }
    else if(P>=10) cout<<-1<<endl;//check if total number is greater than 10 & less than 100, its invalid
    else cout<<P<<endl;//if P is less than 10, it means total answered questions are P
  }
   }
return 0;
}

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

@sougata20702
I also tried to solve this problem several times but its showing wrong answer ,I also cant figure out why…

@sougata20702 - what should be the output for the following test case? Does this help you identify the problem.

Screenshot 2023-06-22 at 4.29.37 PM

Yes, I got that now. Thank you!

@hrishik85 I tried to cover all possible test cases …
I am pasting my code over here can u just put custom test cases and tell me where my code is failing.

include <bits/stdc++.h>

using namespace std;

int main()
{
int t;
cin>>t;
while(t–)
{
int p;
cin>>p;
int y=p;
int x=p;
int num =p;
int ans=0;
int c=0;

std::string str = std::to_string(num);
int k=str.length();
if(k==2||k>1000)
{
cout<<-1<<endl;
}
else {

for(int i=1;i<k-1;i++)
{
if(str[i]!=‘0’)
{
c++;
break;
}
}
if(c>0)
{
cout<<-1<<endl;
}
else
{
ans+=x/100+y%10;
if(ans>10)
{
cout<<-1<<endl;

  }
  else 
     cout<<ans<<endl;

}

}

}
return 0;
}

  1. int k is holding the length of the P string and then in if statement you have written (k>1000). This is the mistake, because you are comparing the length of the string and the value of the string. I think, it should be - (k>4)

  2. if(str[i]!=‘0’) - in this case, what if the input is like - 001

I think these are some problems.

@sougata20702 my bad that should be p>1000 instead of k and for your 2nd point i’d like to explain
my loop is running from (int i=1;i<k-1;i++) and im checking if there is any number present between the first and last element , sob that i can conclude if its a valid number or not …
btw thanks for going thro my code and giving a feedback.

if u have the correct code to this problem then pls share , it would be a great help.!!

// @anish1301 This is the code that I have written. I think it will help you a little.

include
include
using namespace std;

int main()
{
int t;
cin>>t;
while(t–)
{
int P;
cin>>P;
if(P>1000) cout<<-1<<endl;
else if(P==0) cout<<0<<endl;
else {
if(P>=100) {
if(P%100>(10-(P/100))) cout<<-1<<endl;
else if(P%100 == 0) cout<<P/100<<endl;
else cout<<P%100+P/100<<endl;
}
else if(P>10) cout<<-1<<endl;
else cout<<P<<endl;
}
}
return 0;
}

is this code working for all test cases ??

@anish1301 That’s correct code.

1 Like