zoukankan      html  css  js  c++  java
  • Find the Lost Sock (异或算法)

    Alice bought a lot of pairs of socks yesterday. But when she went home, she found that she has lost one of them. Each sock has a name which contains exactly 7 charaters.

    Alice wants to know which sock she has lost. Maybe you can help her.

    Input

    There are multiple cases. The first line containing an integer n (1 <=n <= 1000000) indicates that Alice bought n pairs of socks. For the following 2*n-1 lines, each line is a string with 7 charaters indicating the name of the socks that Alice took back.

    Output

    The name of the lost sock.

    Sample Input

    2
    aabcdef
    bzyxwvu
    bzyxwvu
    4
    aqwerty
    eas fgh
    aqwerty
    easdfgh
    easdfgh
    aqwerty
    aqwerty
    2
    0x0abcd
    0ABCDEF
    0x0abcd

    Sample Output

    aabcdef
    eas fgh
    0ABCDEF

    运用异或运算的一个性质。
    X^X=0;相同数字异或答案为0;
    所以最后的情况变成A^B^B^C^C^D^D=A^0^0^0=A

    把一串分成单个的整数进行异或运算,结果很快就出来了

    #include<stdio.h>
    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF)
        {
             getchar();                     
             int i,j;
             int a[8]={0};
             char s[10];
             for(i=0;i<2*n-1;i++)
             {
                  gets(s);
                  for(j=0;j<7;j++)
                  a[j]=a[j]^s[j];
             }
             for(i=0;i<7;i++)
             printf("%c
    ",a[i]);
        }
    }
  • 相关阅读:
    Java Mockito 笔记
    J2EE 练习题
    CXF 教程 (二)
    CXF 教程(一)
    Java 动态代理
    常用 Git 命令汇总
    JXL 简单示例
    Visual Studio 技巧
    TreeView双击节点而不改变节点的折叠/展开状态
    称3次,找出坏鸡蛋
  • 原文地址:https://www.cnblogs.com/mayouyou/p/8996288.html
Copyright © 2011-2022 走看看