Advertisement
Guest User

вектор

a guest
Apr 27th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <functional>
  5. #include <algorithm>
  6. #include <random>
  7. #include <numeric>
  8. using namespace std;
  9.  
  10. double myfunction (double x, double y) {return x+ y*y;} // складывает квадраты элементов вектора
  11.  
  12. double myfunction1 (double x, double y) {return x + abs (y);} // для оператора <
  13.  
  14. bool comp (vector <double> a, vector <double> b) // сравнивает евклидову норму векторов
  15. {
  16. return accumulate(a.begin(), a.end(), 0, myfunction) < accumulate(b.begin(),b.end(), 0, myfunction);
  17. }
  18.  
  19. bool operator< (vector <double> a, vector <double> b)
  20. {
  21. return accumulate(a.begin(), a.end(), 0, myfunction1) < accumulate(b.begin(),b.end(), 0, myfunction1);
  22. }
  23.  
  24. int main() {
  25.  
  26. vector <vector <double>> a (5, vector <double> (5));
  27. cout <<"vvedite diapazon"<<endl;
  28. int b;
  29. cin >>b;
  30. for (int i=0; i<5; i++)
  31. {
  32. for (int j=0; j<5; j++)
  33. {
  34. a[i][j] = rand () % (2*b +1) - b;
  35. }
  36. }
  37. for (int i=0; i<5; i++)
  38. {
  39. for (int j=0; j<5; j++)
  40. {
  41. cout << a[i][j]<< " ";
  42. }
  43. cout << endl;
  44. }
  45.  
  46.  
  47. /*double x = *min_element(a[0].begin(),a[0].end() );
  48. double y = *max_element (a[0].begin(),a[0].end());
  49. for (int i=1; i<5;i++)
  50. {
  51. if (x > *min_element(a[i].begin(),a[i].end()))
  52. x = *min_element(a[i].begin(),a[i].end());
  53. if (y < *max_element (a[i].begin(),a[i].end()))
  54. y = *max_element (a[i].begin(),a[i].end());
  55. }
  56. cout << y - x <<endl;*/
  57.  
  58.  
  59. /*for (int i=0; i<2; i++)
  60. {
  61. for (int j=0; j<5; j++)
  62. {
  63. cout << a[i][j]<< " ";
  64. }
  65. cout << endl;
  66. }
  67. if (a[0]<a[1])
  68. cout << "yes"<<endl;
  69. else cout << "no"<<endl;*/
  70.  
  71.  
  72. /*sort (a.begin(),a.end(),comp);
  73. for (int i=0; i<5; i++)
  74. {
  75. for (int j=0; j<5; j++)
  76. {
  77. cout << a[i][j]<< " ";
  78. }
  79. cout << endl;
  80. }*/
  81.  
  82.  
  83. vector <vector <double> > c = a;
  84. for (int i=0;i<5;i++)
  85. {
  86. auto x = sqrt (accumulate (a[i].begin(),a[i].end(),0,myfunction));
  87. cout << x << endl;
  88. transform (a[i].begin(),a[i].end(),c[i].begin(), [&x](double y){return y/x;} );
  89. }
  90. for (int i=0; i<5; i++)
  91. {
  92. for (int j=0; j<5; j++)
  93. {
  94. cout << c[i][j]<< " ";
  95. }
  96. cout << endl;
  97. }
  98. cout << accumulate(c[0].begin(), c[0].end(), 0.0, myfunction) << endl;
  99. return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement