Basic division of number by 2 method! c++ string Implementation


#1

//LOGIC is to do basic school division of number by 2 and count (remainder %2==1);
if(count>1)return 0;
else u know what to return?

int Solution::power(string A) {
if(A==“1”)return 0;
int count=0;

while(2)
{
   // cout<<A<<endl;
    bool cx=true;
    for(int i=0;i<A.size();i++)if(A[i]!='0'){cx=false;}
    if(cx)break;
    string temp="";
    if(A.size()>=2){
        int i=0;
        string st="";
        int ct=0;
        bool rem=false;
       
        while(i<A.size())
        {
        
        if(rem){
            
            int num=10+(A[i]-'0');
            if(num%2==1)rem=true;
            else rem=false;
            
           if(num%2==1&&i==A.size()-1){count++;if(count>1)return 0;}
           
           num/=2;
            string s="";
             while(num>0){
                 int t=num%10;
                 s+=(t+'0');
                 num/=10;
             }
             reverse(s.begin(),s.end());
             temp+=s;
        }
        else{
        
        if(ct==0){
            if(A[i]>='2'){
                int num=A[i]-'0';
                if(num%2==1)rem=true;
                else rem=false;
                if(num%2==1&&i==A.size()-1){count++;if(count>1)return 0;}
                char c=num/2+'0';
                temp+=c;
            }
            else {
               if(i!=A.size()-1){
                   temp+='0';
                  if(A[i]!='0'){ st+=A[i];
                   ct++;}
                   
               }
               else
               {
                 int num=A[i]-'0';
                 
                if(num%2==1){count++;if(count>1)return 0;}
                char c=num/2+'0';
                temp+=c;  
               }
            }
            
        }else
        {
            
            st+=A[i];
            int num=(st[0]-'0')*10+(st[1]-'0');
            if(num%2==1)rem=true;
            else rem=false;
             if(num%2==1&&i==A.size()-1){count++;if(count>1)return 0;}
             num/=2;
             string s="";
             while(num>0){
                 int t=num%10;
                 s+=(t+'0');
                 num/=10;
             }
             reverse(s.begin(),s.end());
             temp+=s;
            
            st="";ct=0;
        }
        }
        i++;
        
        }
        
        
    }
    else
    {
      int num=A[0]-'0';
                if(num%2==1){count++;if(count>1)return 0;}
                char c=num/2+'0';
                temp+=c;
    }
    
    A=temp;
}
return 1;

}