fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. typedef std::vector < std::vector<int>> VecVec;
  5.  
  6. VecVec DivArray(std::vector<int>& vec, int div){
  7. VecVec ret;
  8. int Count = vec.size() / div;
  9. int Elem = vec.size() % div;
  10. int i = 0;
  11.  
  12. for (int j = 0; j < div;j++) {
  13. ret.push_back(std::vector<int>());
  14. for (int k = 0; k < Count + (j < Elem ? 1 : 0); k++){
  15. ret[j].push_back(vec[i++]);
  16. }
  17. }
  18.  
  19. return ret;
  20.  
  21. }
  22.  
  23.  
  24. int main(){
  25. std::vector<int> vec;
  26. static const int N[]{10, 15, 10, 10,10};
  27. static const int Div[]{3, 4, 11, 1,10};
  28. int i = 0;
  29.  
  30. for (int j = 0; j < 5; j++){
  31. i = 0;
  32. vec.clear();
  33. vec.resize(N[j]);
  34. for (auto& o : vec)o = i++;
  35.  
  36. auto r = DivArray(vec, Div[j]);
  37. std::cout << "N=>" << N[j] << " Div=>" << Div[j];
  38. for (auto& v : r){
  39. std::cout << " [";
  40. for (i = 0; i < v.size(); i++){
  41. std::cout << v[i];
  42. if (i != v.size() - 1) std::cout << ',';
  43. }
  44. std::cout << ']';
  45. }
  46. std::cout << std:: endl;
  47. }
  48. return 0;
  49. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
N=>10 Div=>3 [0,1,2,3] [4,5,6] [7,8,9]
N=>15 Div=>4 [0,1,2,3] [4,5,6,7] [8,9,10,11] [12,13,14]
N=>10 Div=>11 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] []
N=>10 Div=>1 [0,1,2,3,4,5,6,7,8,9]
N=>10 Div=>10 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]