以下是引用C_printf在2014-1-6 15:34:22的发言:
没人帮你,我帮你!
程序代码:
没人帮你,我帮你!
程序代码:
#define NUMS 8
int comp(const void* a1,const void * a2)
{
return *(int*)a1-*(int*)a2;
}
int _tmain(int argc, _TCHAR* argv[])
{
int nums[NUMS] = {1,1,1,3,3,3,2,2};
int isPair[NUMS] = {0};
int pos1 = 0,pos2 = 1;
int total = 0;
qsort(nums,NUMS,sizeof(int),comp);//排序
while(pos2 < NUMS)
{
if(nums[pos2] / nums[pos1] >= 3)
{
isPair[pos1] = isPair[pos2] = 1;
pos1++;
pos2++;
total++;
}
else
{
pos2++;
}
}
printf("%d\n",NUMS - total);
getchar();
return 0;
}1、这个问题没你想的那么简单,只是一遍顺序扫描并不能在所有情况下找到最优解。这是图论中的最大匹配问题。
2、即使是顺序扫描你的代码逻辑也是错误的。isPair数组在你这里只是个摆设,没用。如果说你用楼主的样例测试正确,那只是个巧合而已。

重剑无锋,大巧不工








