我已经重构了您的代码并对其进行了注释。
主要有以下几点:
- 计算子数组大小时,必须向上取整
-
arr 的索引需要从 0 继续递增(即它不重置为 0)
以下应该可以,但我没有测试它[请原谅无偿的风格清理]:
// Lets call the original integer array with size n: arr
// n is the size of arr
// k is the number of subarrays wanted
// round up the size of the subarray
int subsize = (n + (k - 1)) / k;
int list_of_subArrays[k][subsize];
int arridx = 0;
int subno = 0;
// process all elements in original array
while (1) {
// get number of remaining elements to process in arr
int remain = n - arridx;
// stop when done
if (remain <= 0)
break;
// clip remaining count to amount per sub-array
if (remain > subsize)
remain = subsize;
// fill next sub-array
for (int subidx = 0; subidx < remain; ++subidx, ++arridx)
list_of_subArrays[subno][subidx] = arr[arridx];
// advance to next sub-array
++subno;
}
更新:
是的,这会将数组划分为 n 个子数组,但不会将其平均划分。假设有一个大小为 10 的数组,并且想将其分成 9 个子数组。那么 8 个子数组将有 1 个原始数组的元素,但一个子数组需要有 2 个元素。
您的原始代码存在一些错误 [已在上述示例中修复]。即使我是为自己做这件事,上述内容也将是让某些事情发挥作用的第一步。
在您最初的问题中,您 确实 说:“每个数组必须具有 大约 相同的大小”。但是,这里有列表子数组的物理大小[仍然是向上取整的值]。
但是,我可能会说“均匀分布”之类的东西,以进一步阐明您的意图。也就是说,您希望最后一个子数组/存储桶不是“短”[大幅度]。
鉴于此,代码开始时有些相同,但需要更复杂一些。这还是有点粗糙,可能会进一步优化:
#include <stdio.h>
#ifdef DEBUG
#define dbgprt(_fmt...) printf(_fmt)
#else
#define dbgprt(_fmt...) /**/
#endif
int arr[5000];
// Lets call the original integer array with size n: arr
// n is the size of arr
// k is the number of subarrays wanted
void
fnc2(int n,int k)
{
// round up the size of the subarray
int subsize = (n + (k - 1)) / k;
int list_of_subArrays[k][subsize];
dbgprt("n=%d k=%d subsize=%d\n",n,k,subsize);
int arridx = 0;
for (int subno = 0; subno < k; ++subno) {
// get remaining number of sub-arrays
int remsub = k - subno;
// get remaining number of elements
int remain = n - arridx;
// get maximum bucket size
int curcnt = subsize;
// get projected remaining size for using this bucket size
int curtot = remsub * curcnt;
// if we're too low, up it
if (curtot < remain)
++curcnt;
// if we're too high, lower it
if (curtot > remain)
--curcnt;
// each bucket must have at least one
if (curcnt < 1)
curcnt = 1;
// each bucket can have no more than the maximum
if (curcnt > subsize)
curcnt = subsize;
// last bucket is the remainder
if (curcnt > remain)
curcnt = remain;
dbgprt(" list[%d][%d] --> arr[%d] remain=%d\n",
subno,curcnt,arridx,remain);
// fill next sub-array
for (int subidx = 0; subidx < curcnt; ++subidx, ++arridx)
list_of_subArrays[subno][subidx] = arr[arridx];
}
dbgprt("\n");
}