fork(4) download
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. int main(){
  8.  
  9. cout.setf(ios::fixed);
  10. cout.setf(ios::showpoint);
  11. cout.precision(3);
  12.  
  13. int i, terms;
  14.  
  15. cout << "Enter the number of terms to approximate (or zero to quit):\n";
  16. cin >> terms;
  17.  
  18. while(1){
  19.  
  20. if (terms == 0){
  21. return 0;
  22. }
  23.  
  24. if (terms > 0){
  25.  
  26. double partial = 0;
  27.  
  28. for (i = 0; i < terms; i++)
  29. if (i % 2 == 0)
  30. partial -= (pow(-1,terms))/((2.0 * i) + 1);
  31. else
  32. partial += (pow(-1,terms))/((2.0 * i) + 1);
  33.  
  34. double newPi = 4 * partial;
  35.  
  36. cout << "The approximation is " << newPi << " using " << terms << " terms.\n";
  37.  
  38. }
  39.  
  40. cout << "Enter the number of terms to approximate (or zero to quit):\n";
  41. cin >> terms;
  42.  
  43. }
  44.  
  45. return 0;
  46. }
Time limit exceeded #stdin #stdout 5s 3472KB
stdin
Standard input is empty
stdout
Enter the number of terms to approximate (or zero to quit):