思路: 由于只有一个画笔的颜色编号出现了一次,其余的都出现了两次,所以可以使用异或运算来找出拿走的画笔的颜色编号。

具体步骤如下:

  1. 读取剩下的画笔个数 N-1 和剩下的画笔的颜色编号数组 A;
  2. 初始化一个变量 result,将其赋值为 0;
  3. 遍历数组 A,将 result 与数组中的每个元素进行异或运算;
  4. 输出 result,即为拿走的画笔的颜色编号。

代码实现如下:

#include using namespace std;

int main() { int n; cin >> n;

int a[n];
for (int i = 0; i < n; i++) {
    cin >> a[i];
}

int result = 0;
for (int i = 0; i < n; i++) {
    result ^= a[i];
}

cout << result << endl;

return 0;

原文地址: https://www.cveoy.top/t/topic/iSin 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录