fork download
  1. #include <iostream>
  2. #include <array>
  3. #include <algorithm>
  4. #include <numeric>
  5.  
  6. template <typename T, typename ... AT>
  7. int sumMatched (T A, AT ... args)
  8. {
  9. std::array <T, sizeof ... (AT)> nums { args ... };
  10. auto bg { std::begin (nums) }, ed { std::end (nums) }, res = std::find (bg, ed, A);
  11. return std::accumulate (
  12. ed == res ? bg : ed == (res = std::find (++res, ed, A)) ? bg : ++res,
  13. ed, T {});
  14. }
  15.  
  16. int main ()
  17. {
  18. int A = 1, x1 = 1, x2 = 1, x3 = 3; //допишешь сам сколько надо переменных
  19. std::cout << sumMatched (A, x1, x2, x3) << std::endl;
  20.  
  21. return 0;
  22. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
3