fork download
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4.  
  5. double x[12000] = {0}, y[12000] = {0}, z[12000] = {0};
  6.  
  7. double f(double x, double y, double z)
  8. {
  9. return 6*x;//2*z-y+x-2;
  10. }
  11.  
  12. double Y(double x)
  13. {
  14. double C1 = 1, C2 = 1;
  15. return pow(x, 3) + C1*x + C2;//C1*exp(x)+C2*x*exp(x)+x;
  16. }
  17.  
  18. double L(int n)
  19. {
  20. double s = 0;
  21. for (int i = 0; i < n; i++)
  22. {
  23. s += sqrt(pow(x[i]-x[i+1], 2) + pow(y[i]-y[i+1], 2));
  24. }
  25. return s;
  26. }
  27.  
  28. void d2(double x0, double y0, double z0, double b, int n)
  29. {
  30. double tau = ((b-x0)+.0)/(n+.0);
  31. x[0] = x0; y[0] = y0; z[0] = z0;
  32. double k1, k2, k3, k4;
  33. double m1, m2, m3, m4;
  34. //cout << "\tn = " << n << "\ntau = " << tau << endl;
  35. for (int i = 0; i <= n; i++)
  36. {
  37. x[i] = x0 + i*tau;
  38. //cout << "x_i = " << x0+i*tau << endl;
  39. m1 = z[i];
  40. k1 = f(x[i], y[i], z[i]);
  41. m2 = z[i]+tau*k1/2.;
  42. k2 = f(x[i] + tau/2., y[i] + tau*m1/2., z[i] + tau*k1/2.);
  43. m3 = z[i]+tau*k2/2.;
  44. k3 = f(x[i] + tau/2., y[i] + tau*m2/2., z[i] + tau*k2/2.);
  45. m4 = z[i]+tau*k3;
  46. k4 = f(x[i] + tau, y[i] + tau*m3, z[i] + tau*k3);
  47. y[i+1] = y[i] + tau * (m1 + 2.*m2 + 2.*m3 + m4)/6.;
  48. z[i+1] = z[i] + tau * (k1 + 2.*k2 + 2.*k3 + k4)/6.;
  49. }
  50. }
  51.  
  52. double f2(double x0, double y0, double beta, double b, int n)
  53. {
  54. d2(x0, y0, beta, b, n);
  55. return L(n);
  56. }
  57.  
  58. double gs(double x0, double y0, double b, int n)
  59. {
  60. double x11 = -100, x22 = 100, x1, x2;
  61. double md = (1 + sqrt(5)) / 2, eps = 1e-6;
  62. int i = 0;
  63. //std::cout << "i a b |a - b| \n";
  64. while (fabs(x11 - x22) >= eps)
  65. {
  66. x1 = x22 - (x22 - x11) / md;
  67. x2 = x11 + (x22 - x11) / md;
  68. if (f2(x0, y0, x1, b, n) >= f2(x0, y0, x2, b, n)) x11 = x1; else x22 = x2;
  69. //std::cout << "\n" << ++i << " " << x11 << " " << x22 << " " << fabs(x11 - x22) << "\n";
  70. }
  71. return (x11+x22)/2;
  72. }
  73.  
  74. int main() {
  75. double x0, y0, z0, b, n, eps = 1e-8;
  76. //cout << "n = ";
  77. cin >> n;
  78. //cout << "x0 = ";
  79. cin >> x0;
  80. //cout << "y0 = ";
  81. cin >> y0;
  82. //cout << "z0 = ";
  83. cin >> z0;
  84. //cout << "b = ";
  85. cin >> b;
  86. d2(x0, y0, z0, b, n);
  87. cout << "x_i \t y_i \t \t Y_i \t \t |Y_i - y_i|\n";
  88. for (int i = 0; i <= 10; i++)
  89. {
  90. cout << x[i*int(n/10)] << "\t\t " << y[i*int(n/10)]
  91. << " \t\t " << Y(x[i*int(n/10)])
  92. << " \t " << fabs(Y(x[i*int(n/10)]) - y[i*int(n/10)]) << endl;
  93. }
  94. double zL, zL_;
  95. zL_ = gs(x0, y0, b, n/2);
  96. zL = gs(x0, y0, b, n);
  97. cout << "\n=\nn = " << n << "\nmin zL = " <.20< zL << "\nmin zL_ = " << zL_
  98. << "\nmin L = " << L(n) << endl;
  99. while (fabs(zL-zL_) > eps)
  100. {
  101.  
  102. zL_ = zL;n*=2;
  103. zL = gs(x0, y0, b, n);
  104. d2(x0, y0, zL, b, n);
  105. cout << "\n=\nn = " << n << "\nmin zL = " << zL << "\nmin zL_ = " << zL_
  106. << "\nmin L = " << L(n) << endl;
  107.  
  108. }
  109.  
  110. return 0;
  111. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
10
0 1 1
1
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:97:44: error: no match for ‘operator<’ (operand types are ‘std::basic_ostream<char>’ and ‘double’)
  cout << "\n=\nn = " << n << "\nmin zL = " <.20< zL << "\nmin zL_ = " << zL_
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
prog.cpp:97:44: note: candidate: operator<(int, double) <built-in>
prog.cpp:97:44: note:   no known conversion for argument 1 from ‘std::basic_ostream<char>’ to ‘int’
In file included from /usr/include/c++/6/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/6/bits/char_traits.h:39,
                 from /usr/include/c++/6/ios:40,
                 from /usr/include/c++/6/ostream:38,
                 from /usr/include/c++/6/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/6/bits/stl_pair.h:437:5: note: candidate: template<class _T1, class _T2> constexpr bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
     operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
     ^~~~~~~~
/usr/include/c++/6/bits/stl_pair.h:437:5: note:   template argument deduction/substitution failed:
prog.cpp:97:45: note:   ‘std::basic_ostream<char>’ is not derived from ‘const std::pair<_T1, _T2>’
  cout << "\n=\nn = " << n << "\nmin zL = " <.20< zL << "\nmin zL_ = " << zL_
                                             ^~~
In file included from /usr/include/c++/6/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/6/bits/char_traits.h:39,
                 from /usr/include/c++/6/ios:40,
                 from /usr/include/c++/6/ostream:38,
                 from /usr/include/c++/6/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/6/bits/stl_iterator.h:298:5: note: candidate: template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
     operator<(const reverse_iterator<_Iterator>& __x,
     ^~~~~~~~
/usr/include/c++/6/bits/stl_iterator.h:298:5: note:   template argument deduction/substitution failed:
prog.cpp:97:45: note:   ‘std::basic_ostream<char>’ is not derived from ‘const std::reverse_iterator<_Iterator>’
  cout << "\n=\nn = " << n << "\nmin zL = " <.20< zL << "\nmin zL_ = " << zL_
                                             ^~~
In file included from /usr/include/c++/6/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/6/bits/char_traits.h:39,
                 from /usr/include/c++/6/ios:40,
                 from /usr/include/c++/6/ostream:38,
                 from /usr/include/c++/6/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/6/bits/stl_iterator.h:355:5: note: candidate: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)
     operator<(const reverse_iterator<_IteratorL>& __x,
     ^~~~~~~~
/usr/include/c++/6/bits/stl_iterator.h:355:5: note:   template argument deduction/substitution failed:
prog.cpp:97:45: note:   ‘std::basic_ostream<char>’ is not derived from ‘const std::reverse_iterator<_Iterator>’
  cout << "\n=\nn = " << n << "\nmin zL = " <.20< zL << "\nmin zL_ = " << zL_
                                             ^~~
In file included from /usr/include/c++/6/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/6/bits/char_traits.h:39,
                 from /usr/include/c++/6/ios:40,
                 from /usr/include/c++/6/ostream:38,
                 from /usr/include/c++/6/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/6/bits/stl_iterator.h:1137:5: note: candidate: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorR>&)
     operator<(const move_iterator<_IteratorL>& __x,
     ^~~~~~~~
/usr/include/c++/6/bits/stl_iterator.h:1137:5: note:   template argument deduction/substitution failed:
prog.cpp:97:45: note:   ‘std::basic_ostream<char>’ is not derived from ‘const std::move_iterator<_IteratorL>’
  cout << "\n=\nn = " << n << "\nmin zL = " <.20< zL << "\nmin zL_ = " << zL_
                                             ^~~
In file included from /usr/include/c++/6/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/6/bits/char_traits.h:39,
                 from /usr/include/c++/6/ios:40,
                 from /usr/include/c++/6/ostream:38,
                 from /usr/include/c++/6/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/6/bits/stl_iterator.h:1143:5: note: candidate: template<class _Iterator> bool std::operator<(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorL>&)
     operator<(const move_iterator<_Iterator>& __x,
     ^~~~~~~~
/usr/include/c++/6/bits/stl_iterator.h:1143:5: note:   template argument deduction/substitution failed:
prog.cpp:97:45: note:   ‘std::basic_ostream<char>’ is not derived from ‘const std::move_iterator<_IteratorL>’
  cout << "\n=\nn = " << n << "\nmin zL = " <.20< zL << "\nmin zL_ = " << zL_
                                             ^~~
In file included from /usr/include/c++/6/string:52:0,
                 from /usr/include/c++/6/bits/locale_classes.h:40,
                 from /usr/include/c++/6/bits/ios_base.h:41,
                 from /usr/include/c++/6/ios:42,
                 from /usr/include/c++/6/ostream:38,
                 from /usr/include/c++/6/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/6/bits/basic_string.h:5150:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
     operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^~~~~~~~
/usr/include/c++/6/bits/basic_string.h:5150:5: note:   template argument deduction/substitution failed:
prog.cpp:97:45: note:   ‘std::basic_ostream<char>’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
  cout << "\n=\nn = " << n << "\nmin zL = " <.20< zL << "\nmin zL_ = " << zL_
                                             ^~~
In file included from /usr/include/c++/6/string:52:0,
                 from /usr/include/c++/6/bits/locale_classes.h:40,
                 from /usr/include/c++/6/bits/ios_base.h:41,
                 from /usr/include/c++/6/ios:42,
                 from /usr/include/c++/6/ostream:38,
                 from /usr/include/c++/6/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/6/bits/basic_string.h:5163:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
     operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^~~~~~~~
/usr/include/c++/6/bits/basic_string.h:5163:5: note:   template argument deduction/substitution failed:
prog.cpp:97:45: note:   ‘std::basic_ostream<char>’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
  cout << "\n=\nn = " << n << "\nmin zL = " <.20< zL << "\nmin zL_ = " << zL_
                                             ^~~
In file included from /usr/include/c++/6/string:52:0,
                 from /usr/include/c++/6/bits/locale_classes.h:40,
                 from /usr/include/c++/6/bits/ios_base.h:41,
                 from /usr/include/c++/6/ios:42,
                 from /usr/include/c++/6/ostream:38,
                 from /usr/include/c++/6/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/6/bits/basic_string.h:5175:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const _CharT*, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
     operator<(const _CharT* __lhs,
     ^~~~~~~~
/usr/include/c++/6/bits/basic_string.h:5175:5: note:   template argument deduction/substitution failed:
prog.cpp:97:45: note:   mismatched types ‘const _CharT*’ and ‘std::basic_ostream<char>’
  cout << "\n=\nn = " << n << "\nmin zL = " <.20< zL << "\nmin zL_ = " << zL_
                                             ^~~
In file included from /usr/include/c++/6/bits/ios_base.h:46:0,
                 from /usr/include/c++/6/ios:42,
                 from /usr/include/c++/6/ostream:38,
                 from /usr/include/c++/6/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/6/system_error:200:3: note: candidate: bool std::operator<(const std::error_code&, const std::error_code&)
   operator<(const error_code& __lhs, const error_code& __rhs) noexcept
   ^~~~~~~~
/usr/include/c++/6/system_error:200:3: note:   no known conversion for argument 1 from ‘std::basic_ostream<char>’ to ‘const std::error_code&’
/usr/include/c++/6/system_error:274:3: note: candidate: bool std::operator<(const std::error_condition&, const std::error_condition&)
   operator<(const error_condition& __lhs,
   ^~~~~~~~
/usr/include/c++/6/system_error:274:3: note:   no known conversion for argument 1 from ‘std::basic_ostream<char>’ to ‘const std::error_condition&’
prog.cpp:97:53: error: invalid operands of types ‘double’ and ‘const char [12]’ to binary ‘operator<<’
  cout << "\n=\nn = " << n << "\nmin zL = " <.20< zL << "\nmin zL_ = " << zL_
                                                  ~~~^~~~~~~~~~~~~~~~~
stdout
Standard output is empty