• HDU2095


    2095 find your present (2)

    Problem Description

      In the new year party, everybody will get a "special present".Now it's your turn to get your special present, a lot of presents now putting on the desk, and only one of them will be yours.Each present has a card number on it, and your present's card number will be the one that different from all the others, and you can assume that only one number appear odd times.For example, there are 5 present, and their card numbers are 1, 2, 3, 2, 1.so your present will be the one with the card number of 3, because 3 is the number that different from all the others.

    Input

      The input file will consist of several cases. Each case will be presented by an integer n (1<=n<1000000, and n is odd) at first. Following that, n positive integers will be given in a line, all integers will smaller than 2^31. These numbers indicate the card numbers of the presents.n = 0 ends the input.

    Output

     

    For each case, output an integer in a line, which is the card number of your present

    这个题目,主要见鬼的就是它的内存限制,我做的那个内存就限制在1024K,而开一个数组来记录的话,是很容易爆掉的,于是就有一个位运算的方法。一个数如果异或0的话,是本身,而如果异或两次同一个数,那么这个数就会被抵消掉,而题目正好有个ODD,即唯一的数为奇数次,其余均出现偶数次,因此,用位运算把所有出现偶数次的数全部抵消掉,剩余的就是那个唯一数了。

    #include<cstdio>
    int main()
    {
        int n,m;
        while(scanf("%d",&n)&&n!=0)
        {   int s=0;
            for (int i=1;i<=n;i++)
            {
                scanf("%d",&m);
                s=s^m;
            }
            printf("%d\n",s);
        }
    }

  • 相关阅读:
    Python学习第42天(ftp习题实现day4)
    Python学习第41天(ftp习题实现day3)
    Python学习第40天(ftp习题实现day2)
    Python学习第39天(ftp习题实现day1)
    Python学习第38天(文件处理,os、sys、pickle模块复习)
    Python学习第37天(socketserver继承原理、通讯加密)
    Python学习第36天(socketserver模块导入)
    Python学习第35天(粘包)
    个人读书笔记04—业务用例图
    命令模式
  • 原文地址:https://www.cnblogs.com/kkrisen/p/2770074.html
Copyright © 2020-2023  润新知