俄式乘法 c语言实现

#include 
long muti(long n,long m);
int main()
{
  printf("%ld",muti(1000,1000));


  return 0;


}

long muti(long n,long m)
{
  if(n==1) return m;
  else
  {
    if(n%2==0)
    {
      return muti(n>>1,m<<1);
    }
    else
    {
      return muti((n-1)>>1,m<<1)+m;
    }

  }

}

注:n>>1,m<<1分别为除以2 乘以2

Leave a Reply

Your email address will not be published. Required fields are marked *