给定一个数组,除了一个数出现1次之外,其余数都出现3次。找出出现一次的数。
如:{1, 2, 1, 2, 1, 2, 7}, 找出7.
格式:
第一行输入一个数n,代表数组的长度,接下来一行输入数组A[n],(输入的数组必须满足问题描述的要求),最后输出只出现一次的数。
要求:
你的算法只能是线性时间的复杂度,并且不能使用额外的空间哦~
样例1
输入:
4 0 0 0 5
输出:
5
#include<stdio.h> int main(){ int *A,n; int count=0; scanf("%d",&n); A=(int*)malloc(n*sizeof(int)); for(int i = 0;i<n;i++){ scanf("%d ",&A[i]); } for(int j=0;j<n;j++){ for(int m=0;m<n;m++){ if(A[m]==A[j]) count++; } if(count==1) printf("%d",A[j]); count=0; } return 0; }