fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int countSheep(const vector<int>& sheep) {
  6. int count = 0;
  7.  
  8. for (int s : sheep) {
  9. if (s == 1) { // true → sheep present
  10. count++;
  11. }
  12. }
  13. return count;
  14. }
  15.  
  16. int main() {
  17. vector<int> sheep = {
  18. 1, 1, 1, 0,
  19. 1, 1, 1, 1,
  20. 1, 0, 1, 0,
  21. 1, 0, 0, 1,
  22. 1, 1, 1, 1,
  23. 0, 0, 1, 1
  24. };
  25.  
  26. cout << countSheep(sheep); // Output: 17
  27. return 0;
  28. }
  29.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
17