相关疑难解决方法(0)

使用分而治之的方法找到数字的第n个根

我需要有关如何获得某个数字的第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)

c++ algorithm cmath divide-and-conquer

1
推荐指数
1
解决办法
3570
查看次数

标签 统计

algorithm ×1

c++ ×1

cmath ×1

divide-and-conquer ×1