#include <iostream> #include <vector> typedef std::vector < std::vector<int>> VecVec; VecVec DivArray(std::vector<int>& vec, int div){ VecVec ret; int Count = vec.size() / div; int Elem = vec.size() % div; int i = 0; for (int j = 0; j < div;j++) { ret.push_back(std::vector<int>()); for (int k = 0; k < Count + (j < Elem ? 1 : 0); k++){ ret[j].push_back(vec[i++]); } } return ret; } int main(){ std::vector<int> vec; static const int N[]{10, 15, 10, 10,10}; static const int Div[]{3, 4, 11, 1,10}; int i = 0; for (int j = 0; j < 5; j++){ i = 0; vec.clear(); vec.resize(N[j]); for (auto& o : vec)o = i++; auto r = DivArray(vec, Div[j]); std::cout << "N=>" << N[j] << " Div=>" << Div[j]; for (auto& v : r){ std::cout << " ["; for (i = 0; i < v.size(); i++){ std::cout << v[i]; if (i != v.size() - 1) std::cout << ','; } std::cout << ']'; } std::cout << std:: endl; } return 0; }
Standard input is empty
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]