天天看點

一個數組中隻有兩個數字是出現一次, 其他所有數字都出現了兩次。 找出這兩個隻出現一次的數字,程式設計實作。

#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;

}

繼續閱讀