我今天问的是电力计划。所以,我几乎要弄清楚这样做,我想问一下,如何添加,行之前的数字(如1是; 2是;等等)。所以该计划是
#include<iostream>
using namespace std;
int exponentiation(int base, int exponent);
int main(void)
{
for(int i = 0; i <= 10; ++i)
cout << exponentiation(i, i) << '\n';
}
int exponentiation(int base, int exponent){
int result = 1;
for (int i = 0; i < exponent; ++i)
result = result * base;
return result;
}
输出逐行进行
....
.......
..............
我希望看起来像这样:
1 is ....
2 is .......
3 is ..............
我试图制作一个cout循环并添加上面的代码,但它不起作用:
#include<iostream>
using namespace std;
int exponentiation(int base, int exponent);
int main(void) {
for(int c = 1; c < 10; c++)
{
cout << c << " is ";
for(int i = 0; i <= 10; ++i)
cout << exponentiation(i, i) << '\n';
cout << endl;
}
}
int exponentiation(int base, int exponent) {
int result = 1;
for (int i = 0; i < exponent; ++i)
result = result * base;
return result;
}
答案 0 :(得分:0)
您已准备好完整的循环并等待原始程序。为什么需要添加新的?您需要做的就是在打印exponentiation(i, i)
:
int main()
{
for (int i = 0; i <= 10; ++i)
cout << (i+1) << "is: " << exponentiation(i, i) << '\n';
}
这真的很容易!