最大功率可以将数字提高到超过y递归

时间:2016-11-08 01:49:34

标签: c++

我有这个程序分配,其中一部分是试图找到一个数字将达到(x)的最大功率而不超过用户输入的数字不超过(y)。我们在一个函数中使用它。这是整个程序和我拥有的最大功率它只是保持返回0.这是我想要找出的int maxpower(int x,int y)函数

#include <iostream>
#include <cmath>

using namespace std;

// meunue where you can get your options from
void menue() {
    cout << "choose the following options:" << endl;
    cout << "1) Power of x raised by y." << endl;
    cout << "2) Find the max power a number can be raised to." << endl;
    cout << "3) Print out a number with its digits in reversed order." << endl;
    cout << "4) Sum of integers from 1 to n." << endl;
    cout << "5) Product of integers from 1 to n." << endl;
    cout << "6) Quit" << endl;
}

//functions for finding the power usign recursion
int Power(int a, int b) {
    int x = 1, i;
    for (i = 1; i <= b; i++) {
        if (b == 0) {
            return Power(a, b--);
        }
        else {
            x = x * a;
        }
    }
    return x;
}

int maxpower(int n, int max_value) {
    int temp = temp * n;
    if (temp > max_value)
        return 0;
    else return maxpower(n, max_value + 1);
}

int reverse(int number) {
    int lastDigit, numberOfDigits, sign = 1;//sets the sign equal to one
    // if number is less than 0 returns 0
    if (number < 0) {
        return 0;
    }
    else
        //if a number is under 10 than it can not be switched so you times the number     by 10 and switch it.
    if (number < 10)
        return number * sign;

    lastDigit = number % 10;
    number = number / 10;
    numberOfDigits = log10(number) + 1;
    //recursive statement that calls the function
    return (lastDigit * pow(10, numberOfDigits) + reverse(number)) * sign;
}

//finding the sum
int sum(int n) {
    if (n != 0) {
        return n + sum(n - 1);//recursive statement
    }
    else {
        return n;
    }
}

//finding the product
int product(int n) {
    int temp;
    if (n <= 1) {
        return 1;
    }
    else {
        temp = n * product(n - 1);
        // recursive statement setting temp == to recursive statement
        return temp;//returning temp
    }

}


int main() {
    int a;
    int x;
    int y;
    int length = 0;
    int temp;
    int results;
    // calls menue and get prints all the options

    do {
        menue();
        //inserts the choice
        cin >> a;

        cout << "you choose:" << a << endl;//prints the choice out.
        //switch statement that will take account for the number you choose and prints    the results
        switch (a) {
            case 1:
                cout << "enter the number to raise" << endl;
                cin >> x;
                cout << " enter the power to raise to: " << endl;
                cin >> y;
                Power(x, y);
                cout << "the result is:" << Power(x, y) << endl;
                break;

            case 2:
                cout << "Enter the number to raise:" << endl;
                cin >> x;
                cout << "Enter the number not to exceed:" << endl;
                cin >> y;
                maxpower(x, y);
                cout << "the result is:" << maxpower(x, y) << endl;

                break;

            case 3:
                cout << " enter numbers to be reversed by: " << endl;
                cin >> x;
                temp = x;
                while (temp != 0) {
                    length++;
                    temp = temp / 10;
                }
                reverse(x);
                cout << "the result is:" << reverse(x) << endl;
                break;

            case 4:
                cout << "enter the number to sum to: " << endl;
                cin >> x;
                sum(x);
                cout << "the result is:" << sum(x) << endl;

                break;

            case 5:
                cout << "enter the number to multiply to:" << endl;
                cin >> y;
                product(y);
                cout << "the result is:" << product(y) << endl;
                break;
            case 6:
                cout << "good bye!!" << endl;
                break;
        }

    } while (a != 6);
    return 0;
}

1 个答案:

答案 0 :(得分:0)

我认为使用递归来解决这个问题是不必要的。此外,递归正在创建大量开销,而解决它循环工作正常。你必须使用递归吗?如果是这样,那么忽略这个答案:p。但是,您将在下面找到一个可行的解决方案。

请注意#include <math.h>位 - 您需要使用pow(base, exponent)

另外,while(true)绝对不是最好的做法,但只要你有足够的检查来正确地退出循环,那么你就可以了。因此,max_iteration和您正在寻找的实际return语句。

祝你好运!

#include <iostream>
#include <math.h>

int maxpower(int n, int max_value) {
    if ( n > max_value ) return 0;
    int previous, current = 1;
    int max_iteration = 0;

    while (true) {
        if (max_iteration >= 1000) return -1;
        if (pow(n, current) > max_value) {
            return previous;
        }
        previous = current;
        current++;
        max_iteration++;
    }
}

int main() {
    int x;
    int y;
    int result;

    std::cout << "Enter the base: ";
    std::cin >> x;

    std::cout << "Enter the max number x^pow should not exceed: ";
    std::cin >> y;

    result = maxpower(x, y);

    if (result == -1) {
        std::cout << "Max iteration reached." << std::endl;
    }
    else {
        std::cout << result << " is the maximum power such that " << x << "^" << result << " does not exceed " << y << std::endl;
    }
    return 0;
}

作为输出的一个例子:

如果x = 2且y = 32,程序将返回5作为最大功率(即2 ^ 5 = 32并且不大于,但是2 ^ 6> 32)。

编辑:

在我发布所有功能都是递归的时候,我意识到这可能是你的任务要求。无论如何,下面是递归解决方案:

int maxpower_rec_helper(int n, int power, int max_value) {
    if (pow(n, power) > max_value) return power - 1;
    return maxpower_rec_helper(n, power + 1, max_value);
}

int maxpower_rec(int n, int max_value) {
    if ( n > max_value ) return 0;
    return maxpower_rec_helper(n, 1, max_value);
}

您需要辅助功能才能提供初始权力1,以免打扰您max_value

return power - 1;与上面的迭代示例中的return previous;基本相同。