1 double 2 pres(const int n) { 3 double numerator = 2; //分子. 4 double denominator = 1; //分母. 5 double sum = 0; 6 double tmp; 7 8 for(int i = 1; i <= n; i++) { 9 sum += (numerator / denominator); 10 11 tmp = numerator; //临时保存当前分子(以下一个数使用). 12 13 numerator += denominator; //下一个分子为当前(分子分母的和). 14 denominator = tmp; //下一个分母为当前分子. 15 } 16 17 return sum; 18 } 19 //result = 32.6603(前20项)
时间: 2024-10-23 04:14:00