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