Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. #include <string>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <utility>
  8.  
  9. using ::std::cout;
  10. using ::std::cin;
  11. using ::std::string;
  12.  
  13.  
  14. double CoutValueOfPolynomial(int n , int* arr, double val);
  15. std::vector<int> FindDividers(int a);
  16.  
  17.  
  18. int main() {
  19.  
  20. int n;
  21. cin >> n;
  22. int* arr = new int[n + 1];
  23. std::vector<std::pair<int,int>> roots;
  24.  
  25. int temp;
  26.  
  27. for(int i = 0 ; i < n + 1 ; i++){
  28. cin >> temp;
  29. arr[i] = temp;
  30. }
  31.  
  32.  
  33. int a0 = arr[n]; // wspolczynnik a_0
  34. int an = arr[0]; // wspolczynik a_n
  35.  
  36. // dzielniki a0
  37. auto a0_dividers = FindDividers(a0);
  38. // znajduje dzielniki calkowite a0 , ktore sa pierwiastkami
  39. for(auto divider: a0_dividers){
  40. if(CoutValueOfPolynomial(n,arr,divider)==0){
  41. auto p = std::make_pair(divider,1);
  42. roots.push_back(p);
  43. }
  44. }
  45.  
  46. auto an_dividers = FindDividers(an);
  47. double dtemp;
  48.  
  49. for(auto an_divider: an_dividers){
  50. for(auto a0_divider: a0_dividers){
  51. dtemp = (double)a0_divider / an_divider;
  52. if(CoutValueOfPolynomial(n,arr,dtemp) == 0){
  53. auto p = std::make_pair(a0_divider, an_divider);
  54. roots.push_back(p);
  55. }
  56. }
  57. }
  58.  
  59.  
  60.  
  61.  
  62.  
  63. cout << roots.size() << "\n";
  64. for(auto val: roots){
  65. cout << val.first << " " << val.second << "\n";
  66. }
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. delete arr;
  74.  
  75.  
  76. return 0;
  77. }
  78.  
  79. double CoutValueOfPolynomial(int n , int* arr, double val){
  80. double result = 0;
  81. for (int i = 0 ; i < n + 1; i++){
  82. result += (pow(val, n- i ) * arr[i]);
  83. }
  84. return result;
  85. }
  86.  
  87.  
  88.  
  89. std::vector<int> FindDividers(int a){
  90. std::vector<int> dividers;
  91. if (a > 0){
  92. for(int i = 1 ; i <= a ; i++){
  93. if(a % i == 0){
  94. dividers.push_back(i);
  95. }
  96. }
  97. for(int i = -1 ; i >= -a ; i--){
  98. if(a % i == 0){
  99. dividers.push_back(i);
  100. }
  101. }
  102. } else if (a < 0){
  103. for(int i = -1 ; i >= a ; i--){
  104. if(a % i == 0){
  105. dividers.push_back(i);
  106. }
  107. }
  108. for(int i = 1 ; i <= -a ; i++){
  109. if(a % i == 0){
  110. dividers.push_back(i);
  111. }
  112. }
  113. }
  114.  
  115. return dividers;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement