天天看点

一个数组中只有两个数字是出现一次, 其他所有数字都出现了两次。 找出这两个只出现一次的数字,编程实现。

#include<stdio.h>

#include<stdlib.h>

void find_num(int a[],int n){

int i, j,tmp = 0;

for (i = 0; i < n-1; i++){

for (j = 0; j < n - i - 1; j++){

if (a[j] > a[j + 1]){

tmp = a[j];

a[j] = a[j + 1];

a[j + 1] = tmp;

}

}

}

for (int x = 0; x < n; x+=2){

if (a[x] == a[x + 1]){

continue;

}

else if(a[x] != a[x+1]){

printf("%d\n", a[x]);

x++; //和下一个相比较

}

}

}

int main(){

int a[] = { 1, 1, 2, 2, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8 };

int len = sizeof(a)/sizeof(a[0]);

find_num(a, len);

system(“pause”);

return 0;

}