微软面试100系列 第32题

#include <stdio.h>
#include <math.h>
#include <malloc.h>

void QuckSort(int *a, int low, int high);
int  FindPos(int *a, int left, int right);
void PrintInt(int *a, int len);
int  SumOfArray(int *a, int len);
void Find_Small_distance(int *a, int *b, int *source, int len, int size);
void assign_array(int *a, int *b, int *src, int len, int small, int big);
int  check_success(int *a, int *b, int len, int dis);
void Create_source(int *a, int *b, int *source, int len);

int main()
{
 //int array1[] = {100,99,98,1,2, 3};
 //int array2[] = {1, 2, 3, 4,5,40};
 int array1[] = {1, 4, 5, 800};
 int array2[] = {2, 3, 6, 700};
 int *source = NULL;
 int len = 0;
 int diss = 0;

 len = sizeof(array1) / sizeof(int);
 source = (int *)malloc(2 * len * sizeof(int));
 printf("The  previous array1 is :/n");
 PrintInt(array1, len);
 printf("The  previous array1 is :/n");
 PrintInt(array2, len);
 
 Create_source(array1, array2, source, len);
 Find_Small_distance(array1, array2, source, len, len);
 diss = SumOfArray(array1, len) - SumOfArray(array2, len);
 printf("The small distance between array1 and array2 is : %d/n", diss);
 printf("Now the array1 is :/n");
 PrintInt(array1, len);
 printf("Now the array1 is :/n");
 PrintInt(array2, len);
 free(source);
 return 0;
}


void QuckSort(int *a, int low, int high)
{
 int pos = 0;

 if( low < high)
 {
  pos = FindPos(a, low , high - 1);
  QuckSort(a, low, pos);
  QuckSort(a, pos + 1, high);
 }
}

int  FindPos(int *a, int left, int right)
{
 int key = 0;
 int low, high;

 key = a[left];
 low = left;
 high = right;

 while(low < high)
 {
  while(low < high && a[high] > key)
  {
   high--;
  }
  if(low < high)
  {
   a[low] = a[high];
   low++;
  }
  while(low < high && a[low] <= key)
  {
   low++;
  }
  if(low < high)
  {
   a[high] = a[low];
   high--;
  }
 }
 a[low] = key;
 return low;
}

void PrintInt(int *a, int len)
{
 int i = 0;

 for(i = 0; i < len; ++i)
 {
  printf("%-5d", a[i]);
 }
 puts("");
}

int  SumOfArray(int *a, int len)
{
 int sum = 0;
 int i = 0;

 for(i = 0; i < len; ++i)
 {
  sum += a[i];
 }
 return sum;
}
//重新调整数组a 和 b
void assign_array(int *a, int *b, int *src, int len, int small, int big)
{
 int i = 0;
 int j = 0;
 for(i = 0; i < 2 * len; ++i)
 {
  if(i < len)
  {
   if(i == len - 1)
   {
    a[i] = big;
   }
   else
   {
    a[i] = src[i];
   }
  }
  else
  {
   if(j == len - 1)
   {
    b[j] = small;
   }
   else
   {
    b[j] = src[i - 1];
    j++;
   }
  }
 }
}

void Create_source(int *a, int *b, int *source, int len)
{
 int i = 0, j = 0;
 
 for(i = 0; i < 2 * len; ++i)
 {
  if(i < len)
  {
   source[i] = a[i];
  }
  else
  {
   source[i] = b[j];
   j++;
  }
 }
 QuckSort(source, 0, 2 * len);
}
//找a[i] 和 b[i] 之差接近 dis/ 2 的a[i] 和 b[i]
int  check_success(int *a, int *b, int len, int dis) 
{
 int i = 0, j = 0;
 int dif = 0, max_dis = 0;
 int indexx = 0, indexy = 0;
 int temp = 0;

 for(i = 0; i < len; ++i)
 {
  for(j = 0; j < len; ++j)
  {
   dif = a[i] - b[j];
   if(dif < 0)
   {
    break;
   }
   else
   {
    if(dif > max_dis)
    {
     indexx = i;
     indexy = j;
     max_dis = dif;
    }
   }
  }
 }
 
 if(max_dis > 0 && max_dis < dis)
 {
  temp = a[indexx];
  a[indexx] = b[indexy];
  b[indexy] = temp;
  return 1;
 }
 else
 {
  return 0;
 }
}
//主要算法
void Find_Small_distance(int *a, int *b, int *source, int len, int size)
{
 int  i =0, j = 0;
 int sum_a = 0, sum_b = 0, dis = 0;
 int big = 0, small = 0;

 QuckSort(a, 0, size);
 QuckSort(b, 0, size);
 big = source[2 * len - 1];
 small = source[2 * len - 2];
 assign_array(a, b, source, len, small, big);
 sum_a = SumOfArray(a, size);
 sum_b = SumOfArray(b, size);
 
 dis =sum_a - sum_b;
 if(dis > 0)
 {
  if(1 == check_success(a, b, len -1 , dis))
  {
   Find_Small_distance(a, b, source, len - 1, size); 
  }
  else if(len < 1)
  {
   return;
  } 
 }
 else if(dis < 0)
 {
  if(1 == check_success(b, a, len - 1, -dis))
  {
   Find_Small_distance(b, a, source, len - 1, size); 
  }
  else if(len < 1)
  {
   return;
  }
 }
 else
 {
  return;
 } 
}s

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值