fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. // Funkcja wczytująca stopień i współczynniki wielomianu
  8. vector<int> wczytajWielomian(int &n) {
  9. cin >> n;
  10.  
  11. vector<int> a(n + 1);
  12. for (int i = n; i >= 0; i--) {
  13. cin >> a[i];
  14. }
  15.  
  16. return a;
  17. }
  18.  
  19. // Funkcja obliczająca wartość wielomianu (algorytm naiwny)
  20. int obliczWartosc(const vector<int> &a, int n, int x) {
  21. int wynik = 0;
  22.  
  23. for (int i = 0; i <= n; i++) {
  24. wynik += a[i] * pow(x, i);
  25. }
  26.  
  27. return wynik;
  28. }
  29.  
  30. int main() {
  31. int n, x;
  32.  
  33. vector<int> a = wczytajWielomian(n);
  34. cin >> x;
  35.  
  36. cout << "W(" << x << ")= " << obliczWartosc(a, n, x);
  37.  
  38. return 0;
  39. }
  40.  
Success #stdin #stdout 0.01s 5284KB
stdin
3 1 2 3 4 2
stdout
W(2)= 26