fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. // Funkcja ustawiająca stopień wielomianu i jego współczynniki
  6. void ustawWielomian(int &n, vector<int> &a)
  7. {
  8. n = 3; // stopień wielomianu
  9. a.resize(n + 1);
  10.  
  11. a[3] = 1; // a3 = 1
  12. a[2] = 2; // a2 = 2
  13. a[1] = 3; // a1 = 3
  14. a[0] = 4; // a0 = 4
  15. }
  16.  
  17. // Funkcja obliczająca wartość wielomianu algorytmem naiwnym
  18. int obliczWielomian(int n, const vector<int> &a, int x)
  19. {
  20. int wynik = 0;
  21.  
  22. for (int i = 0; i <= n; i++)
  23. {
  24. int potega = 1;
  25. for (int j = 0; j < i; j++)
  26. {
  27. potega *= x;
  28. }
  29. wynik += a[i] * potega;
  30. }
  31.  
  32. return wynik;
  33. }
  34.  
  35. int main()
  36. {
  37. int n;
  38. vector<int> a;
  39. int x = 2; // wartość x
  40.  
  41. ustawWielomian(n, a);
  42.  
  43. int wynik = obliczWielomian(n, a, x);
  44.  
  45. cout << "Stopien wielomianu = " << n << endl;
  46. cout << "x = " << x << endl;
  47. cout << "W(" << x << ") = " << wynik << endl;
  48.  
  49. return 0;
  50. }
  51.  
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
Stopien wielomianu = 3
x = 2
W(2) = 26