zoukankan      html  css  js  c++  java
  • 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);
        }
    }

  • 相关阅读:
    seriviceWorker 小结
    Number.prototype.toLocalString() js
    浏览器h5新建文件 保存到本地(相当于浏览器写文件)
    ios 当margin-left margin-right 超过设备宽度
    数组变char
    字符窜转数字
    stream 的方式遍历
    LinkedHashSet 去掉重复数据
    前端判断是否为空字符窜
    前端去掉空格的方法
  • 原文地址:https://www.cnblogs.com/kkrisen/p/2770074.html
Copyright © 2011-2022 走看看