fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <iterator>
  5. #include <vector>
  6.  
  7. int main()
  8. {
  9. std::vector<std::vector<int>> a;
  10. for (std::string s; std::getline(std::cin, s); ) {
  11. std::istringstream iss(s);
  12. std::istream_iterator<int> itr(iss), end;
  13. a.emplace_back(itr, end);
  14. }
  15. const int m = a.size();
  16. const int n = a[0].size();
  17. std::vector<std::vector<int>> b(m + 1, std::vector<int>(n + 1, 0));
  18. for (int i = 0; i < m; ++i) {
  19. for (int j = 0; j < n; ++j) {
  20. b[i][j] = a[i][j];
  21. b[i][n] += a[i][j];
  22. b[m][j] += a[i][j];
  23. b[m][n] += a[i][j];
  24. }
  25. }
  26. for (auto &v : b) {
  27. for (auto x : v) {
  28. std::cout << x << " ";
  29. }
  30. std::cout << std::endl;
  31. }
  32. }
  33.  
Success #stdin #stdout 0s 15240KB
stdin
1 2 3
4 5 6
stdout
1 2 3 6 
4 5 6 15 
5 7 9 21