求一个算法~~~~~~~想到头爆了~~~~~~
比如一列数字:1 1 1 3 3 3 2 2 如果A是B的三倍或者三倍以上,则分为一组,已经分好的不能重复分,不能分成组的各为一组,求这组数据最少可以分成几组;则上列数据是5组;
我自己想过用扫描的方法来做,但是好像行不通~~~求一个算法~~~~~~
程序代码:
#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;
}