使上面的代码输出为整数
时间: 2024-02-23 20:00:25 浏览: 26
好的,以下是修改后的代码:
```c++
#include<iostream>
#include<cmath> // 引入 pow 函数
using namespace std;
int Mypow(int a,int b);
int main() {
int x, y;
cout << "请输入两个整数,中间用空格隔开:" << endl;
cin >> x >> y;
int result = Mypow(x, y);
cout << x << "的" << y << "次方为:" << result << endl;
return 0;
}
int Mypow(int a, int b) {
return pow(a, b); // 直接调用 pow 函数求解
}
```
修改后的代码中,我们引入了cmath头文件,并调用了其中的pow函数,用来直接求解a的b次方。函数返回值也修改为整型,这样输出结果就会为整数了。