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课堂练习7
    Java课后练习6
    Java课后练习5
    Java课后练习4
    Java课后练习3
    课堂练习
    求和程序实验报告
    大道至简第二章读后感
    课堂作业例子
  • 原文地址:https://www.cnblogs.com/mayouyou/p/8996288.html
Copyright © 2011-2022 走看看