選大王?收菜?

#include<stdio.h>
//猴子選大王吧
//直接模擬,循環一圈後會亂?把未選出的數字放到覆蓋到原來的數組上
//設置K來確定不亂
void fun(int *A,int *B,int temp,int n)
{
	int i,j,k,ib,stop;
	k=temp;
    stop=n;
	i=stop+1;
	j=stop;
	ib=0;
	while(ib<n)
	{
		if(i>=stop)
		{		
			stop=j;
			i=j=0;
		}
		if(--k!=0)//k的作用確保開始後的順序不會亂
		{
			A[j++]=A[i++];
		}
		else
		{
			B[ib++]=A[i++];
			k=temp;
		}
	}
}

int main()
{
	 int  num[10]={1,2,3,4,5,6,7,8,9,10};//測試數據 
	 int here[10];
	 fun(num,here,2,10);
	 for(int i=0;i<10;i++)
		 printf("%d ",here[i]);
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章