int arr[] = {2,3,1,0,2,5,3}; int len = (int) sizeof(arr) / sizeof(*arr); for (int i = 0;i<len;i++) { int number = arr[i]; for (int j = i;j<len;j++) { if (number == arr[j]) { printf("%d",number); return; } } } printf("未找到");
int arr[] = {2,3,1,0,2,5,3}; int len = (int) sizeof(arr) / sizeof(*arr);
//排序 int i,j,temp; for (i=1;i<len;i++){ temp = arr[i]; for (j=i;j>0 && arr[j-1]>temp;j--){ arr[j] = arr[j-1]; } arr[j] = temp; }
//遍历 int index = 0; bool result = false; for (int i = 0;i<len-1;i++) { int number = arr[index]; int next = arr[index+1]; if (number == next) { result = true; printf("--%d---",number); } index = index+1; }
int arr[] = {2,3,1,0,2,5,3}; int len = (int) sizeof(arr) / sizeof(*arr);
if(len <= 0 ){ return; }
for(int i = 0; i < len ; i++){ if (arr[i] < 0 || arr[i] > len-1 ) { return; } int number = arr[i]; while(number != i){ int nextNum = arr[number]; if(number == nextNum){ printf("%d",number); break; } int temp = number; arr[i] = nextNum; arr[number] = temp; } }
4、辅助数组+遍历
1 2 3 4 5 6 7 8 9 10 11 12 13
int arr[] = {2,3,1,0,2,5,3}; int len = (int) sizeof(arr) / sizeof(*arr);
int arr1[len+1]; for (int i = 0;i<len;i++) { int number = arr[i]; int tagerNumber = arr1[number]; if(number == tagerNumber){ printf("----%d\n",number); }else{ arr1[number] = number; } }