Search This Blog

Showing posts with label Subtraction and Multiplication in Integer Array. Show all posts
Showing posts with label Subtraction and Multiplication in Integer Array. Show all posts

Thursday, 25 August 2016

SPOJ - Bishops Problem In C++

SPOJ - Bishops Problem In C++

Solution:-

// cumbersome task to implement in C/C++, but nice feeling after AC.

Logic:-
if(grid_size==1)
    Answer=1;
else 
    Answer=(size of grid*2) -2;


#include<bits/stdc++.h>
using namespace std;
int arr[200];
int i,len,carry,num,pos;

int main()
{
    string s;
    while(getline(cin,s))
    {
        len=i=carry=num=pos=0;
        memset(arr,0,sizeof(arr));
        len=s.length();
        pos=0;
        for(i=len-1;i>=0;i--,pos++)     //copy the string to integre array in reverse order
            arr[pos]=s[i]-'0';

        carry=0;
        for(i=0;i<len;i++)                   // double the integer array
        {
            num=(arr[i]*2) + carry;
            arr[i]=num%10;
            carry=num/10;
        }
        while(carry)
        {
            arr[i]=carry%10;
            carry/=10;
            if(carry)
                i++;
        }
        pos=i;                   // subtract 2 from the array
        i=0;
        if(arr[i]>=2)
            arr[i]-=2;
        else
        {
            arr[i]+=8;
            i++;
            arr[i]--;
        }

        while(1)
        {
            if(arr[i]>=0 || i==pos)
                break;
            else
            {
                arr[i]=9;
                i++;
                arr[i]--;
            }
        }
        while(arr[pos]==0)
            pos--;
        if(pos<0)  // if the grid size was 1, than pos becomes less than zero
            cout<<1;
        for(i=pos;i>=0;i--)
            cout<<arr[i];
        cout<<endl;
    }
    return 0;

}