- 论坛徽章:
- 0
|
本帖最后由 zhangbo357 于 2015-04-24 16:33 编辑
大致思想就是将两个数组a b的差放到另一个数组c,再对c数组求和,再用该和减c数组中的元素,如果减了之后绝对值变小了就将对应位置的a b数组元素交换,直到最后,就这么简单
测试了下 貌似正确- #include <stdio.h>
- #include <math.h>
- int main()
- {
- int a[5] = {6,5,8,4,9};
- int b[5] = {5,8,5,1,6};
- int c[5] = {0};
- int sum1 = 0,sum2 = 0,suma = 0,sumb = 0;
- int i;
- for(i = 0;i < 5;i++)
- {
- c[i] = a[i]-b[i];
- sum1 +=c[i];
- }
- printf("%d\n",sum1);
- for( i = 0;i < 5;i++)
- {
- sum2 = abs(sum1);
- if(abs(sum1 - c[i]*2) < sum2)
- {
- sum1 -= c[i]*2;
-
- a[i] ^= b[i];
- b[i] ^= a[i];
- a[i] ^= b[i];
- }
- suma += a[i];
- sumb += b[i];
- printf("%d %d\n",a[i],b[i]);
- }
-
- printf("%d\n",suma-sumb);
-
- return 0;
- }
复制代码 |
|