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

W(2) = 26