异或特性

<p>
<img src="https://img-blog.csdn.net/20150429141402450?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbDU1aXVtaW5n/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /></p><p><strong>一开始看到这个题目我就知道是找不同(数据中单独的一个数),然后我就想通过两个for循环一个一个比,看到n的范围 我就知道可能超时,就想到了异或运算;</strong></p><p><strong><span style="color: rgb(102, 102, 102); font-family: arial, tahoma, 'Microsoft Yahei', 宋体, sans-serif; font-size: 17.1428565979004px; line-height: 32px;">1.与1异或,可以使特定位翻转,</span><br style="color: rgb(102, 102, 102); font-family: arial, tahoma, 'Microsoft Yahei', 宋体, sans-serif; font-size: 17.1428565979004px; line-height: 32px;" /><span style="color: rgb(102, 102, 102); font-family: arial, tahoma, 'Microsoft Yahei', 宋体, sans-serif; font-size: 17.1428565979004px; line-height: 32px;">2.与0异或,保留其值</span><br style="color: rgb(102, 102, 102); font-family: arial, tahoma, 'Microsoft Yahei', 宋体, sans-serif; font-size: 17.1428565979004px; line-height: 32px;" /><span style="color: rgb(102, 102, 102); font-family: arial, tahoma, 'Microsoft Yahei', 宋体, sans-serif; font-size: 17.1428565979004px; line-height: 32px;">3.交换两值,不使用临时变量。a=a^b;b=b^a;a=a^b;</span>
</strong></p><p><strong><span style="color: rgb(102, 102, 102); font-family: arial, tahoma, 'Microsoft Yahei', 宋体, sans-serif; font-size: 17.1428565979004px; line-height: 32px;">例如</span></strong></p><p><strong><span style="color: rgb(102, 102, 102); font-family: arial, tahoma, 'Microsoft Yahei', 宋体, sans-serif; font-size: 17.1428565979004px; line-height: 32px;">0^1=1,1^3=2;</span></strong></p><p><strong><span style="color: rgb(102, 102, 102); font-family: arial, tahoma, 'Microsoft Yahei', 宋体, sans-serif; font-size: 17.1428565979004px; line-height: 32px;"> x=2;y=3;</span></strong></p><p><strong></strong></p><pre id="recommend-content-581568281" accuse="aContent" class="recommend-text mb-10" style="margin-top: 0px; margin-bottom: 10px; padding: 0px; font-family: arial, 'courier new', courier, 宋体, monospace; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 17.1428565979004px; line-height: 24px; background-color: rgb(241, 254, 221);">x^y----->0010----->0011
        结果 0001

相同为0,不同为1
特性

a=a^b;
    b=a^b;      {此语句为交换a,b值的}
    a=a^b;
例如

a^b^c^c^b^a=a^a^b^b^c^c^c=0^0^0^c=c(a,b.c代表任意整数)

题目代码


 
#include <stdio.h>
int main()
{
	int i,n,s;
	while(scanf("%d",&n),n)
	{
		s=0;
		while(n--)
		{
			scanf("%d",&i);
			s^=i;
		}
		printf("%d\n",s);
	}
	return 0;
}
;;;;;;;;;;;;;;;
;;;;;;;
<strong>不知道是题目数据的原因还是出题人故意考别人智商,这题如果输出所输入数据的最大值竟然也会AC.</strong>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值