fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int main() {
  6. int x = 2;
  7.  
  8. // Wspolczynniki: a3, a2, a1, a0
  9. vector<int> a = {1, 2, 3, 4};
  10.  
  11. int stopien = 3;
  12. int wynik = a[0];
  13.  
  14. cout << "Schemat Hornera – kolejne kroki:\n";
  15. cout << "x = " << x << endl;
  16. cout << "Start: wynik = a3 = " << wynik << endl;
  17.  
  18. for (int i = 1; i <= stopien; i++) {
  19. cout << "\nKrok " << i << ":" << endl;
  20. cout << "wynik = " << wynik << " * " << x
  21. << " + a" << (stopien - i)
  22. << " (" << a[i] << ")" << endl;
  23.  
  24. wynik = wynik * x + a[i];
  25.  
  26. cout << "wynik = " << wynik << endl;
  27. }
  28.  
  29. cout << "\nW(" << x << ") = " << wynik << endl;
  30.  
  31. return 0;
  32. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Schemat Hornera – kolejne kroki:
x = 2
Start: wynik = a3 = 1

Krok 1:
wynik = 1 * 2 + a2 (2)
wynik = 4

Krok 2:
wynik = 4 * 2 + a1 (3)
wynik = 11

Krok 3:
wynik = 11 * 2 + a0 (4)
wynik = 26

W(2) = 26