应该是很简单的水题,不过再简单思路不对也是白搭
第一感觉,很不幸,
Memory Limit Exceeded 版本
#include <iostream> using namespace std; int num[1000001]; int main() { int n,temp,i,m; while(scanf("%d",&n)&&n) { memset(num,0,sizeof(num)); m=n; while(m--) { scanf("%d",&temp); num[temp]++; } for(i=1;i<=n;i++) if(num[i]==1) printf("%d/n",i); } }
后来才发现,原来还可以用异或这东西
主要原理 1个数异或他自己 =0
1个数异或0 = 他自己
理解了上面这个就好办了,题目中说的其他数字都出现偶数次,只一个数出现一次
如果 将 0异或他们全部,结果将是只出现一次的那个数
于是有了下面的
#include <iostream> using namespace std; int main() { int n,temp,i; while(scanf("%d",&n)&&n) { int result=0; while(n--) { scanf("%d",&temp); result=result^temp; } cout<<result<<endl; } }
AC 开阔思路啊开阔思路