講的詳細點,我是大一新生,小白,盡可能把調試好的代碼發一遍
#include
int f(int,int);
int power(int, int);
int main()
{
int k,n;
std::cout << "please input n:";
std::cin >> n;
std::cout << "please input k:";
std::cin >> k;
std::cout << "result = " << f(n,k) << std::endl;
return 0;
}
// 1^k + 2^k + 3^k + ... + n^k
int f(int n, int k)
{
int sum = 0;
for (int i = 1; i <= n; ++i)
{
sum = sum + power(i, k);
}
return sum;
}
// m^n
int power(int m, int n)
{
int product = 1;
for (int i = 0; i < n; ++i)
{
product = product * m;
}
return product;
}