fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. // funkcja zwracajaca wartosc wielomianu (algorytm naiwny)
  7. int obliczWartosc(const vector<int> &a, int stopien, int x) {
  8. int wynik = 0;
  9.  
  10. for (int i = 0; i <= stopien; i++) {
  11. int potega = 1;
  12. for (int j = 0; j < i; j++) {
  13. potega *= x;
  14. }
  15. wynik += a[i] * potega;
  16. }
  17.  
  18. return wynik;
  19. }
  20.  
  21. int main() {
  22. int stopien = 3;
  23. vector<int> a;
  24.  
  25. // wspolczynniki: a0, a1, a2, a3
  26. a.push_back(4); // a0
  27. a.push_back(3); // a1
  28. a.push_back(2); // a2
  29. a.push_back(1); // a3
  30.  
  31. int x = 2;
  32.  
  33. int W = obliczWartosc(a, stopien, x);
  34.  
  35. cout << "Stopien wielomianu: " << stopien << endl;
  36. cout << "a3 = " << a[3] << endl;
  37. cout << "a2 = " << a[2] << endl;
  38. cout << "a1 = " << a[1] << endl;
  39. cout << "a0 = " << a[0] << endl;
  40. cout << "x = " << x << endl;
  41. cout << "W(" << x << ") = " << W << endl;
  42.  
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0.01s 5308KB
stdin
Standard input is empty
stdout
Stopien wielomianu: 3
a3 = 1
a2 = 2
a1 = 3
a0 = 4
x = 2
W(2) = 26