Adding Reversed Numbers (SPOJ)

You can reverse a number like the following technique with numerical operation:

#define ll long long 

ll reverse_num(ll N)
{
    ll ret=0;
    while(N)
    {
        ret= (ret*10) + (N%10);
        N/=10;
    }
    return ret;
}


Another way of string operation to reverse a number:


        string s1;
        char st1[12];

        sf("%s",st1);
        ll l1=strlen(st1);

        bool flag_0;             //-----------to handle leading zero

        //------------------reverse number

        if(st1[l1-1]=='0')
            flag_0=1;
        else
            flag_0=0;

        for(ll i=l1-1;i>=0;i--)
        {
            if(flag_0==0)
                s1+=st1[i];
            else if(st1[i]!='0')
            {
                flag_0=0;
                s1+=st1[i];
            }
        }

     Now s1 string is the reverse number (without leading zero) of st1 array


Comments

Popular posts from this blog

PopIt Serially support page

10405 - Longest Common Subsequence (UVA)