- //以空间换时间
- /*
- arr :数组名
- n :给定的数组长度
- 转移数组中的数据
- */
- void diffarr(int arr[],int n)
- {
- int sn=n,index,*ptr=arr;
- int *pfrom=new int[sn];
- for(int i=0;i<sn;i++)
- *(pfrom+i) =i+1 ;
- srand(time(0));
- for(int k=0;k<sn;)
- {
- index=rand()% sn;
- if(*(pfrom+index)!=0)
- { //从源数据pfrom取,每取一个将其置为0,直至pfrom中所有元素都为0
- *(ptr+k)=pfrom[index];
- pfrom[index]=0;
- k++;
- }
- }
- delete [] pfrom;
- }
- //该片段来自于http://www.codesnippet.cn/detail/130620149761.html
来源: http://www.codesnippet.cn/detail/130620149761.html