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);
        }
    }

  • 相关阅读:
    IOS开发C语言入门如何结合Terminal和Vim开发C语言程序
    如何开发原生的 JavaScript 插件(知识点+写法)
    IOS开发数据持久化篇之文件存储(一)
    PC 端微信扫码注册和登录
    2020最全面的微服务
    RabbitMQ详解
    MongoDB基础篇&集群篇
    从原理上理解MySQL的优化建议
    分布式日志搜集ELK
    MySQL高级篇性能优化
  • 原文地址:https://www.cnblogs.com/kkrisen/p/2770074.html
Copyright © 2011-2022 走看看