大神,求用递归调用求式子
n^1+n^2+...+n^10,其中n=1,2,3
但为什么要用递归呢?递归,能不用则不用!
程序代码:
#include <iostream> unsigned foo( unsigned n, unsigned m ) { if( m == 1 ) return n; return n*( 1 + foo(n,m-1) ); } int main( void ) { std::cout << "n=1, reuslt=" << foo(1,10) << '\n'; std::cout << "n=2, reuslt=" << foo(2,10) << '\n'; std::cout << "n=3, reuslt=" << foo(3,10) << std::endl; }
输出
n=1, reuslt=10
n=2, reuslt=2046
n=3, reuslt=88572