AdiReddy's blog

By AdiReddy, history, 4 years ago, In English

I am getting runtime error for the above problem for shorter input,what can I do.

include

using namespace std;

int num_of_digits(int x) { int count=1; while(x>=10) { x=x/10; count++; } return count; }

int return_number(int arr[],int n) { int number=1,sum=0; for(int i=n-1;i>0;i--) { sum=sum+(number*arr[i]); number=number*10; } if(arr[0]!=0){sum=sum+(number*4);} return sum; }

int next(int x) { int digits=num_of_digits(x); int arr[digits+1]; int i; for (i = digits; i >0; --i) { arr[i]=x%10; x=x/10; } arr[0]=0; i=1;

while(i<=digits)
{
    if(arr[i]<=3) {arr[i]=4;break;}
    if(arr[i]==4) {i++;}
    if(arr[i]==6||arr[i]==5) {arr[i]=7;break;}
    if(arr[i]==7) {i++;}
    if(arr[i]==8||arr[i]==9)
    {
       while(true)
       {
           i--;
         if(arr[i]==4) {arr[i]=7;break;}
         if(arr[i]==0){arr[i]=4;break;}
       }
       break;
    }
}
for(int j=i+1;j<digits+1;j++)
{
    arr[j]=4;
}
int num=return_number(arr,digits+1);
return num;

}

int main() { int l,r; cin>>l>>r; int sum=0; int j=next(l); for (int i = l; i < r+1; ++i) { if(i<=j){sum+=j;} if(i==j){j=next(i+1);} } cout<<sum; }

  • Vote: I like it
  • -27
  • Vote: I do not like it

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Firstly, you can link your submission instead of pasting the code. This helps anyone debugging to see what exactly happened. So for your submission, (assuming it's your most recent one), you could say 82272638.

Second, your code and the input it failed on are simple enough that you should be able to work out what went wrong by hand. You are in the best position to do this, since the code is yours and you understand it better than anyone else. Or if you don't want to do that, you can also use Custom Invocation to run your code. Add some extra print statements for debugging in each step, so you can pinpoint where your code crashes.

This should be enough to figure out what's going wrong.