我需要有关如何获得某个数字的第n个根的帮助.
用户输入他想要root的数字n和数字.我需要在没有cmath lib和分而治之的方法下解决这个问题.
这是我的代码还没有工作:
#include<iostream>
using namespace std;
float pow(float a,float c){
if (a == 0)
return 0;
else if(a == 1)
return 1;
else{
float p = pow(a,(c/2));
if(c%2)
return p*p*a;
else
return p*p;
}
}
int main(){
float a,b;
float c;
cout << "Enter positive number:(base)" << endl;
do{
cin >> a;
}while (a < 0);
cout << "Enter number: (root)" << endl;
cin >> b;
c = 1/b;
cout << "Result:"<<pow(a,c) << endl;
system("pause");
return 0;
} …Run Code Online (Sandbox Code Playgroud)