C++ 如何使用循环增加指数?

C++ 如何使用循环增加指数?,c++,loops,C++,Loops,我想用loop增加指数。例如,X是指数开始的数字,Y是指数结束的数字。Z是基数。 因此,如果输入为: X=1 Y=6 Z=2 输出将是 2 4 6 8 16 32 64 #包括 使用名称空间std; int main() { int x,y,z; coutx; 库蒂; 库茨; 对于(z=z;z您需要循环通过指数从下限(x)到上限(y),并执行操作z^n,其中(n>=x和n您需要循环通过指数从下限(x)到上限(y),并执行操作z^n,其中(n>=x和nfor(int i=x;ifor(int i=

我想用loop增加指数。例如,X是指数开始的数字,Y是指数结束的数字。Z是基数。 因此,如果输入为:

X=1 Y=6 Z=2

输出将是

2 4 6 8 16 32 64

#包括
使用名称空间std;
int main()
{
int x,y,z;
coutx;
库蒂;
库茨;

对于(z=z;z您需要循环通过指数从下限(x)到上限(y),并执行操作z^n,其中(n>=x和n您需要循环通过指数从下限(x)到上限(y),并执行操作z^n,其中(n>=x和n
for(int i=x;i
for(int i=x;i以前的解决方案在每次迭代时都有计算pow(基数,指数)的缺点。
通过迭代计算得到的结果要少得多

#include <iostream>
#include <cmath>
using namespace std;
int main()

{
    int first, last, base;

    cout << "first = ";
    cin >> first;
    cout << "last = ";
    cin >> last;
    cout << "base = ";
    cin >> base;


    int result = pow (base, first);

    for(int exponent = first; exponent <= last; ++exponent){
        cout << base << "^" << exponent << " = " << result << "\n";
        result *= base;
    }
    return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
int first,last,base;
cout>首先;
最后;
cout>base;
int结果=功率(基准,第一);

对于(int-exponent=first;exponent,以前的解决方案在每次迭代时都有计算pow(base,exponent)
的缺点。
通过迭代计算得到的结果要少得多

#include <iostream>
#include <cmath>
using namespace std;
int main()

{
    int first, last, base;

    cout << "first = ";
    cin >> first;
    cout << "last = ";
    cin >> last;
    cout << "base = ";
    cin >> base;


    int result = pow (base, first);

    for(int exponent = first; exponent <= last; ++exponent){
        cout << base << "^" << exponent << " = " << result << "\n";
        result *= base;
    }
    return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
int first,last,base;
cout>首先;
最后;
cout>base;
int结果=功率(基准,第一);

对于(int exponent=first;exponent请测试我的答案并在可能的情况下予以批准。谢谢!请测试我的答案并在可能的情况下予以批准。谢谢!您应该根据您的数据类型限制指数,以防溢出,或者使用更大的类型。@Dan这是一个小练习。我只是想说明如何限制操作次数ns.更改数据类型并不能防止溢出,限制指数的值是另一个练习。很好,没有挖掘,只是指出它,不确定您是否应该根据您的数据类型限制指数以防止溢出,或者使用更大的类型。@Dan这是一个小练习。我只是想说明一下对如何限制操作数进行评级。更改数据类型并不能防止溢出,限制指数的值是另一个练习。所有这些都很好,没有挖掘,只是指出它,不确定您是否知道在哪里
for(int i = x; i <= y; i++) {
    int pow_num = pow(z,i); // This is what you have to do.
}
#include <iostream>
#include <cmath>
using namespace std;
int main()

{
    int first, last, base;

    cout << "first = ";
    cin >> first;
    cout << "last = ";
    cin >> last;
    cout << "base = ";
    cin >> base;


    int result = pow (base, first);

    for(int exponent = first; exponent <= last; ++exponent){
        cout << base << "^" << exponent << " = " << result << "\n";
        result *= base;
    }
    return 0;
}