zoukankan      html  css  js  c++  java
  • AtCoder Beginner Contest 171 E

    E - Red Scarf


    Time Limit: 2 sec / Memory Limit: 1024 MB

    Score : 500500 points

    Problem Statement

    There are NN Snuke Cats numbered 1,2,,N1,2,…,N, where NN is even.

    Each Snuke Cat wears a red scarf, on which his favorite non-negative integer is written.

    Recently, they learned the operation called xor (exclusive OR).

    What is xor?

    They wanted to use this operation quickly, so each of them calculated the xor of the integers written on their scarfs except his scarf.

    We know that the xor calculated by Snuke Cat ii, that is, the xor of the integers written on the scarfs except the scarf of Snuke Cat ii is aiai. Using this information, restore the integer written on the scarf of each Snuke Cat.

    Constraints

    • All values in input are integers.
    • 2N2000002≤N≤200000
    • NN is even.
    • 0ai1090≤ai≤109
    • There exists a combination of integers on the scarfs that is consistent with the given information.

    Input

    Input is given from Standard Input in the following format:

    NN
    a1a1 a2a2  aNaN
    

    Output

    Print a line containing NN integers separated with space.

    The ii-th of the integers from the left should represent the integer written on the scarf of Snuke Cat ii.

    If there are multiple possible solutions, you may print any of them.


    Sample Input 1 Copy

    Copy
    4
    20 11 9 24
    

    Sample Output 1 Copy

    Copy
    26 5 7 22
    
    • 5 xor 7 xor 22=205 xor 7 xor 22=20
    • 26 xor 7 xor 22=1126 xor 7 xor 22=11
    • 26 xor 5 xor 22=926 xor 5 xor 22=9
    • 26 xor 5 xor 7=2426 xor 5 xor 7=24

    Thus, this output is consistent with the given information.

    题意:输入一个n,然后输入n个数字,然后请输出n个数字,让他们的数字XOR的值能组成原来的数组

    解题思路:其实这道题利用到了同一个数XOR偶数次等于0,这个原理,根据XOR的原理,位相同为0,不同为1,我们可以将开始输入的n个数XOR起来,然后输出的时候分别再和输入的元素XOR,就能得到新的元素。

    代码如下:

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #define maxn 200005
    int a[maxn];
    using namespace std;
    int main(void)
    {
        int n;
        scanf("%d",&n);
        int sum=0;
        for(int i=0;i<n;++i)
        {
            scanf("%d",&a[i]);
            sum^=a[i];//n个数XOR和
        }
        for(int i=0;i<n-1;++i)
        {
            printf("%d ",sum^a[i]);//消去a[i]
        }
        printf("%d
    ",sum^a[n-1]);
        
        return 0;
    }
  • 相关阅读:
    macOS 系统下载地址
    docker基本使用-nginx
    adb命令安装apk
    docker基本使用-常用命令
    docker基本使用-安装
    Vue技术点整理-vue.config.js
    flutter环境部署
    Android webview 问题记录
    Node 使用webpack编写简单的前端应用
    前端api管理工具YApi
  • 原文地址:https://www.cnblogs.com/Mangata/p/13308236.html
Copyright © 2011-2022 走看看