题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=528
解题思路:对一个数连续异或(^)偶数次结果为0。
代码如下:
#include <cstdio>
#include <cctype>
int Input() //按字符输入
{
int c,res,flag(1);
while( !isdigit(c=getchar()) && c!='-' ); //忽略非数字字符
c=='-' ? flag=0 : res=c-'0' ;
while( isdigit(c=getchar()) )
res = res*10 + c-'0'; //计算数值大小
return flag ? res : -res ;
}
int main()
{
int ans,n,x;
while(~scanf("%d",&n))
{
ans = 0 ;
while(n--)
{
x=Input();
ans^=x;
}
printf("%d\n",ans);
}
return 0;
}

本文介绍了一个ACM竞赛中的经典异或问题,并提供了一种简洁高效的解决方案。通过使用C语言实现,文章展示了如何利用异或运算的特性来解决实际问题。
646

被折叠的 条评论
为什么被折叠?



