The last one

Title Description

Taurus chose a positive integer X, then write it on the blackboard. Then every day he would erase the last digit of the current, until he erased all the digits. Throughout the process, all beef will appear on the board through a digital record, and obtains the sum of their sum.
E.g. X = 509, appear on the board 509 is in turn over numbers, 50, 5, and their It is 564.
beef now given a sum, beef want you to find a positive integer X after the result of the process is the sum.

Enter a description:

The input comprises a positive integer sum (1 ≤ sum ≤ 10 ^ 18)

Output Description:

Outputs a positive integer that satisfies the condition of X, if X is not so, the output of -1. 

Problem-solving ideas:

After a series of wiping operations: abc + ab + a = 564;

Soku: (a * 100 + b * 10 + c) + (a * 10 + b) + (a) = 564;

That is: 111 * a + 11 * b + 1 * c = 564;

For digit has 1111 * a + 11 * b + 1 * c + * d = x;

首先0<a<10,b<10,c<10,d<10;

Len demand function can be given a 1-bit

After len minus bit x 1 * a, b can be determined in the same manner

code show as below:

#include <the iostream> 
#include < String > 
#include <the cmath> 
#include <The iomanip>
 / * 
Use Case: 
837592744927492746 
the corresponding output would be: 
-1 
Your output: 
753833470434743470 
* / 
the using namespace STD; 
Long  Long give ( int len );
 int main () 
{ 
    String  SUM ;
     int A [ 100 ], Top = 0 ;
     Long  Long num_sum = 0 ; 
    CIN >> SUM ; 

    for( Int I = 0 ; I < SUM .size (); I ++ ) 
    { 
       // num_sum + = (SUM [I] - '0') * POW (10.0, sum.size () -. 1-I); this does not know Why wrong 
       num_sum = num_sum * 10 + ( SUM [I] - ' 0 ' ); // this is a 
    } // I = 12 is when an error occurs, I> 10
      // when when i = 0 num_sum = Why not 799999999999999999 800000000000000000
     // Long Long T = num_sum;
    // COUT << num_sum; return 0; // why the conversion is wrong out integer
     // sum_size sum.size = (); 
    for ( int I = SUM.size();i>=1;i--)
    {
        a[top]=num_sum/give(i);
        num_sum=num_sum-a[top]*give(i);
        top++;
    }
    //cout<<top<<'\n';
    for(int i=0;i<top;i++)
    {
    if(a[i]>=10)
    {
    cout<<-1;return 0;
    }
    }
    for(int i=0;i<top;i++)
    {
    
    cout<<a[i];
    }

//    for(int i=11;i>=3;i--)
//    cout<<give(i)<<endl;
}

long long give(int len)//提供len位1
{
    long long sum=0;
    while(len-->0)
    {
        sum=sum*10+1;
    }
    return sum;
}//837592744927492746

 

Guess you like

Origin www.cnblogs.com/cstdio1/p/11031064.html