#include "stdafx.h"
#include "iostream"
using namespace std;
//计算 1+1/2+1/3+............+1/n 的和
double sum(int n) {
if (n == 1) { //递归出口,当n = 1时,返回1
return 1.0;
}
else {
return (1.0 / n) + sum(n - 1); //当n>1时 返回1/n + (1/(n-1)+1/(n-2)+........+1/2+1)
}
}
void main()
{
int m = 1;
int n;
cout << "输入n" << endl;
cin >> n;
cout << sum(n);
}
运行