用C++输入两个整数 x 和 n ,计算 x 的 n 次幂并输出,如果 n 为 0 则输出 1
时间: 2023-05-12 18:03:23 浏览: 72
好的,以下是代码:
```c
#include <stdio.h>
int main() {
int x, n, result = 1;
printf("请输入两个整数 x 和 n:");
scanf("%d %d", &x, &n);
if (n == 0) {
printf("%d 的 %d 次幂为 1\n", x, n);
} else {
for (int i = 0; i < n; i++) {
result *= x;
}
printf("%d 的 %d 次幂为 %d\n", x, n, result);
}
return 0;
}
```
希望能对你有所帮助!
相关问题
设计一个递归函数,求x的n次幂,其中n为非须整数。在main函数中输入x和一个整数n,输出x的n次幂。
可以通过以下递归函数实现:
```c++
double power(double x, int n) {
if (n == 0) {
return 1.0;
}
double half = power(x, n/2);
if (n % 2 == 0) {
return half * half;
} else if (n > 0) {
return half * half * x;
} else {
return half * half / x;
}
}
```
这个函数将n分为n/2和n-n/2两部分,然后递归计算x的n/2次幂。如果n是偶数,则将结果平方即可;否则,需要再乘上一个x。如果n是负数,则需要将x取倒数。
在main函数中,可以像下面这样使用该函数:
```c++
#include <iostream>
using namespace std;
int main() {
double x;
int n;
cout << "请输入x和n: ";
cin >> x >> n;
cout << x << "的" << n << "次幂是: " << power(x, n) << endl;
return 0;
}
```
用c++分治法编写完整代码,实现 pow(x,n),说明: -100.0 < x < 100.0 , n 是 32 位有符号整数,其数值范围是 [−231, 231 − 1] 。即计算 x 的整数 n 次幂函数,要求在输入一个实数 x 和一个整数 n 后,能够计算出值,并进行最终输出,并进行面向对象编程,列出各项本设计要达到的具体的目标,同时给出编写的类及其成员说明;使用流程图、伪代码或文字,说明核心成员函数的运行原理或流程。
本设计要达到的目标:
1. 实现 pow(x,n) 函数,计算 x 的整数 n 次幂;
2. 输入一个实数 x 和一个整数 n,输出计算结果;
3. 使用分治法实现,提高效率;
4. 使用面向对象编程,封装代码,使得易于维护和复用。
设计的类及其成员说明:
1. 类名:MyPow,包含两个私有成员变量 double x 和 int n,分别表示输入的实数和整数;
2. 构造函数:MyPow(double x, int n),用于初始化 x 和 n;
3. 成员函数:double calculate(),用于计算 pow(x,n) 的值,返回类型为 double。
核心成员函数的运行原理或流程:
1. 首先判断 n 的正负性,如果 n 为负数,将 x 取倒数,n 取相反数,这样可以将问题转化为正数幂的计算;
2. 如果 n 为 0,则返回 1;
3. 如果 n 为奇数,则将问题分解为 x * pow(x, n-1) 的计算,即将幂次减 1;
4. 如果 n 为偶数,则将问题分解为 pow(x*x, n/2) 的计算,即将底数平方,幂次减半;
5. 递归执行上述操作,直到 n 为 0 或 1,返回最终的计算结果。
以下是完整代码的实现:
```c++
#include <iostream>
using namespace std;
class MyPow {
private:
double x;
int n;
public:
MyPow(double x, int n) {
this->x = x;
this->n = n;
}
double calculate() {
if (n < 0) {
x = 1 / x;
n = -n;
}
return helper(x, n);
}
double helper(double x, int n) {
if (n == 0) return 1;
if (n == 1) return x;
if (n % 2 == 0) {
double temp = helper(x * x, n / 2);
return temp;
} else {
double temp = helper(x * x, n / 2);
return x * temp;
}
}
};
int main() {
double x;
int n;
cout << "请输入实数 x 和整数 n,用空格隔开:" << endl;
cin >> x >> n;
MyPow myPow(x, n);
double res = myPow.calculate();
cout << "pow(" << x << ", " << n << ") = " << res << endl;
return 0;
}
```
流程图如下所示:
![pow(x,n)流程图](https://cdn.luogu.com.cn/upload/image_hosting/38a3d1a2.png)
伪代码如下所示:
```c++
class MyPow {
private:
double x;
int n;
public:
MyPow(double x, int n) {
this->x = x;
this->n = n;
}
double calculate() {
if (n < 0) {
x = 1 / x;
n = -n;
}
return helper(x, n);
}
double helper(double x, int n) {
if (n == 0) return 1;
if (n == 1) return x;
if (n % 2 == 0) {
double temp = helper(x * x, n / 2);
return temp;
} else {
double temp = helper(x * x, n / 2);
return x * temp;
}
}
}
// 测试代码
int main() {
double x;
int n;
cout << "请输入实数 x 和整数 n,用空格隔开:" << endl;
cin >> x >> n;
MyPow myPow(x, n);
double res = myPow.calculate();
cout << "pow(" << x << ", " << n << ") = " << res << endl;
return 0;
}
```