基础数学问题

时间:2020-06-18 22:39:30   收藏:0   阅读:70

找筷子
技术分享图片

解题思路:

要找出唯一单着的那个数,可以用异或的方式
我们已知 a ^ a = 0 ,对于存在偶数个的数字,迟早会互相抵消,奇数个的数字也会两两抵消,只剩下最后一个答案。

AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

int main(){
	int n;
	int a;
	scanf("%d", &n );
	int ans = 0;
	for( int i = 0 ;i < n ; i++ ){
		scanf("%d", &a );
		ans ^= a;
	}
	printf("%d\n", ans );
	return 0;
}

原文:https://www.cnblogs.com/w-w-t/p/13160346.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!