1 #include<iostream> 2 #include<cmath> 3 using namespace std; 4 5 #define r 1 6 7 /* 8 计算2^n边型的单边边长 9 */ 10 double singleLength(int n) { 11 if (n == 2) 12 return sqrt(2)*r; 13 double sinalphad2 = singleLength(n - 1) / (2 * r); 14 double cosalphad2 = sqrt(1 - sinalphad2*sinalphad2); 15 double sinalphad4 = sqrt((1 - cosalphad2) / 2); 16 double y = 2 * r*sinalphad4; 17 return y; 18 } 19 20 int main() { 21 int n; 22 cout << "最多计算到2^n边型,n="; 23 cin >> n; 24 25 double length = singleLength(n)*pow(2.0,(double)n); 26 cout.precision(20); 27 cout << "估算的周长为:" << fixed << length << endl; 28 cout << "估算的圆周率为:" << fixed << length / (2 * r) << endl; 29 return 0; 30 }
时间: 2024-10-15 08:57:33