- 论坛徽章:
- 0
|
回复 1楼 forrestgang 的帖子
先将数组排序(全排序,两个数组一起排),然后类似如下处理
#include <iostream>
using namespace std;
void swap(int &x, int &y)
{
x = x ^ y;
y = y ^ x;
x = y ^ x;
}
int main()
{
int a[] = {1, 13, 34, 44, 54, 58};
int b[] = {3, 23, 45, 56, 67, 77};
int i, sub;
static int total = 0;
for(i = 5; i >=0 ; i--)
{
sub = b - a;
if(total)
{
if((total > 0 && sub > 0) || (total < 0 && sub < 0))
{
swap(a, b);
total -= sub;
}
else if((total >0 && sub < 0) || (total < 0 && sub > 0))
total += sub;
}
else
{
total = sub;
}
}
cout << "a: " << endl;
for(i = 0; i < 6; i++)
cout << a << " " ;
cout << endl;
cout << "b: " << endl;
for(i = 0; i < 6; i++)
cout << b << " " ;
cout << endl;
cout << "total: " << total << endl;
}
[ 本帖最后由 chzht001 于 2006-11-14 10:49 编辑 ] |
|