实现X*N

#include<iostream>
using namespace std;

double foo(int n,double x)
{
  if(1==n)
  {
	return x;
  }
  else
  {
	if(n%2==0)
	  return foo(n/2,x*x);
	else
	  return x*foo((n-1)/2, x*x);
  }
} 

int main()
{
  int n;
  double x;
  cin >> x >>n;
  cout<< foo(n,x) <<endl;
}

  

时间: 2024-10-08 14:08:17