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

Full text and comments »

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

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

Full text and comments »

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