- 论坛徽章:
- 0
|
本帖最后由 dd8924 于 2016-12-02 13:49 编辑
无聊翻个老贴,我的思路是:
1:2个数组元素组个新数组并排序
2:先把最大的数丢到一个数组里
3:开始循环,分别计算每个数组的和,倒序把新数组的元素丢到和较小的那个数组里.
欢迎指正.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARR_LEN (5)
#define ARR_AREA (100)
static int *g_pArra = NULL, *g_pArrb = NULL;
static int arrInit (const int **ppAddr, int len, int size)
{
int nRet = 0;
if (NULL == ppAddr || !len || !size) {
perror ("bad param!");
nRet = -1;
return nRet;
}
*ppAddr = calloc (len, size);
if (NULL == ppAddr) {
perror ("ppAddr calloc err!");
nRet = -1;
return nRet;
}
return nRet;
}
static int arrElementGet (void)
{
int nElement = 0;
//srand ((int)time(NULL));
nElement = rand () % ARR_AREA;
//printf ("get new element [%d]\n", nElement);
return nElement;
}
static arrFill (int len)
{
int nRet = 0;
int i = 0;
for (i = 0; i < len; ++i) {
g_pArra = arrElementGet();
g_pArrb = arrElementGet();
}
return nRet;
}
static int arrPrint (const int *pArrd, int len)
{
int i = 0;
for (i = 0; i < len; ++i) {
printf ("%d ", pArrd);
}
printf ("\n");
}
static int arrCmp(const void *a, const void *b)
{
return(*(int *)a-*(int *)b);
}
static int arrSumGet (const int *pAddr, int len)
{
int i = 0, sum = 0;
for (i = 0; i < len; ++i) {
sum += pAddr;
}
return sum;
}
int main (int argc, char *argv[])
{
int nRet = 0;
int i = 0, j = 0, k = 0;
int *pArrTmp = NULL;
int nTatolSum = 0;
int nAvg = 0;
int sumA = 0, sumB = 0;
do {
nRet = arrInit (&g_pArra, ARR_LEN, sizeof(int));
if (nRet) {
perror ("g_pArra arrInit error!!!");
break;
}
nRet = arrInit (&g_pArrb, ARR_LEN, sizeof(int));
if (nRet) {
perror ("g_pArrb arrInit error!!!");
break;
}
nRet = arrFill (ARR_LEN);
printf ("arr a:");
arrPrint (g_pArra, ARR_LEN);
printf ("arr b:");
arrPrint (g_pArrb, ARR_LEN);
// make a new array
nRet = arrInit (&pArrTmp, ARR_LEN * 2, sizeof(int));
if (nRet) {
perror ("pArrTmp arrInit error!!!");
break;
}
// fill element from array a and b
for (i = 0; i < ARR_LEN * 2; ++i) {
pArrTmp = (ARR_LEN <= i) ? g_pArrb[i - ARR_LEN] : g_pArra;
}
printf ("arr tmp:");
arrPrint (pArrTmp, ARR_LEN * 2);
// sort
qsort (pArrTmp, ARR_LEN * 2, sizeof (int), arrCmp);
printf ("\nsort arr tmp:");
arrPrint (pArrTmp, ARR_LEN * 2);
nTatolSum = arrSumGet (pArrTmp, ARR_LEN * 2);
nAvg = nTatolSum / 2;
printf ("nTatolSum=%d nAvg=%d \n", nTatolSum, nAvg);
memset (g_pArra, 0x0, ARR_LEN*4);
memset (g_pArrb, 0x0, ARR_LEN*4);
// fill the max element
g_pArra[ARR_LEN - 1] = pArrTmp[ARR_LEN*2 - 1];
j = ARR_LEN - 2; k = ARR_LEN - 1;
for (i = ARR_LEN * 2 - 2; i > 0; --i) {
sumA = arrSumGet (g_pArra, ARR_LEN);
sumB = arrSumGet (g_pArrb, ARR_LEN);
//arrPrint (g_pArra, ARR_LEN);
//arrPrint (g_pArrb, ARR_LEN);
printf ("pArrTmp=%d sumA=%d, sumB=%d \n", pArrTmp, sumA, sumB);
if (sumA >= sumB) {
g_pArrb[k] = pArrTmp;
--k;
}
else {
g_pArra[j] = pArrTmp;
--j;
}
}
printf ("after change arr a:");
arrPrint (g_pArra, ARR_LEN);
printf ("after change arr b:");
arrPrint (g_pArrb, ARR_LEN);
sumA = arrSumGet (g_pArra, ARR_LEN);
sumB = arrSumGet (g_pArrb, ARR_LEN);
printf ("sumA=%d, sumB=%d diff=%d \n", sumA, sumB, sumA-sumB);
}while (0);
if (g_pArra) {
free (g_pArra);
}
if (g_pArrb) {
free (g_pArrb);
}
printf ("app exit!\n");
return nRet;
}
|
|