C++ 在不超过y递归的情况下,数字可以提升到的最大幂
我有这个程序分配,其中一部分是试图找到一个数字的最大功率(x),而不超过用户输入的不超过(y)的数字。我们在函数中使用它。这是整个程序,我有最大功率,它一直返回0。这是我想弄清楚的int-maxpower(intx,inty)函数C++ 在不超过y递归的情况下,数字可以提升到的最大幂,c++,C++,我有这个程序分配,其中一部分是试图找到一个数字的最大功率(x),而不超过用户输入的不超过(y)的数字。我们在函数中使用它。这是整个程序,我有最大功率,它一直返回0。这是我想弄清楚的int-maxpower(intx,inty)函数 #include <iostream> #include <cmath> using namespace std; // meunue where you can get your options from void menue() {
#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;
}
#包括
#包括
使用名称空间std;
//你可以从哪里得到你的选择
void menue(){
cout我认为没有必要使用递归来解决这个问题。此外,递归会产生大量开销,而使用循环来解决这个问题效果很好。你必须使用递归吗?如果是这样,那么忽略这个答案:p。但是你会发现下面的解决方案是可行的
注意#include
位-您需要它来使用pow(基数,指数)
另外,while(true)
肯定不是最佳实践,但只要您有足够的检查来正确地退出循环,那么您就可以了。因此,您要寻找的max\u迭代和实际的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;
}
您需要一个助手函数来提供初始功率1
,以避免干扰您的max\u值
return power-1;
本质上与上面迭代示例中的return previous;
相同。行“int temp=temp*n;”在我看来是错误的。您声明了一个名为temp的int(没有为其分配任何值)。然后您说的是temp=(未初始化值)*n.int temp=n*n;int power=0;if(temp>max_值)返回0;//这是已编辑的版本,现在只打印出1。否则if(tempint temp=temp*n;
仍然是错误的。
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);
}