我想使用循环增加指数。例如,X 是指数开始的数,Y 是指数结束的数。 Z是基数。
所以如果输入是:
X=1 Y=6 Z=2
输出将是
2 4 6 8 16 32 64
#include <iostream>
using namespace std;
int main()
{
int x,y,z;
cout<<"X = ";
cin>>x;
cout<<"Y = ";
cin>>y;
cout<<"Z = ";
cin>>z;
for(z=z;z<=z;z){
for(x=x;x<=y;x++){
}
}
return 0;
}
这就是我能做的。我应该在循环部分做什么?
您需要从下限 (x) 到上限 (y) 遍历指数,并执行操作 z^n where (n >= x and n <= y)。请参阅下面的代码,为了便于阅读,我将 x 重命名为开始,将 y 重命名为完成,将 z 重命名为基础。不要忘记包含 math.h。
#include <iostream>
#include <math.h>
using namespace std;
int main() {
int start, finish, base;
cout << "Start: ";
cin >> start;
cout << "Finish: ";
cin >> finish;
cout << "Base: ";
cin >> base;
// start at lower bound, increase n by 1
// until n is equal to upper bound
for(int n = start; n <= finish; n++) {
cout << base << "^" << n << " = " << pow(base, n) << endl;
}
return 0;
}