zoukankan      html  css  js  c++  java
  • find your present (2)

    此博客链接:https://www.cnblogs.com/ping2yingshi/p/12401693.html

    find your present (2)(41min)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2095

    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.
     
    Sample Input
    5
    1 1 3 2 2
    3
    1 2 1
    0
    题解:
            题意:给出一组数据,找出其中只有单独一个数的数字。
            此题一开始是使用数组做的,但是运行时超时,参考了别人写的,原来使用数组会超时,本题使用异或运算。
           方法:异或运算。
           思路:对于异或运算,相同数异或得零,对于本题,只要出现相同数最后异或运算都得零,只有剩下的单独一个数保留下来即为结果。
           补充:
           异或运算:2的二进制是10,3的二进制是11
                  2和2的异或:相同数字异或为0,不同数字异或为1
                                                                                                    10
                                                                                                    10
                                                                                                  ——
                                                                                                    00
                
                2和3的异或:相同数字异或为0,不同数字异或为1
                                                                                                  10
                                                                                                  11
                                                                                               ——
                                                                                                  01
    代码如下:
    #include<stdio.h>
    #include<math.h>
    #include<stdlib.h>
    #include<string.h>
    
    int main()
    {
        int n;
        while (~scanf_s("%d",&n))
        { 
            if (n == 0)
                break;
            int result=0;
            int num;
            for (int i = 0; i < n; i++)
            {
                scanf_s("%d", &num);
                result ^= num;
            }
            printf("%d
    ",result);
        }
        return 0;
    }

    运行超时数组代码如下:

    #include<stdio.h>
    #include<math.h>
    #include<stdlib.h>
    #include<string.h>
    
    int inum[10000];
    int renum[10000];
    int main()
    {
        int n;
        while (~scanf_s("%d",&n))
        { 
            if (n == 0)
                break;
            for (int i = 0; i < n; i++)
                scanf_s("%d", inum[i]);
            for (int i = 0; i < n; i++)
                renum[inum[i]]++;
            for(int i=0;i<n;i++)
            if(renum[inum[i]]==1)
            printf("%d
    ", inum[i]);
        }
        return 0;
    }
  • 相关阅读:
    自动化测试(Selenium+python)-环境搭建
    Jenkins默认插件
    Jenkins安装
    jdk环境变量配置
    RobotFramework使用chrome打开浏览器,提示chromedriver.exe停止运行
    Java之获取年月日时分秒字符串
    JavaScript验证输入是否为空
    轮播图简单实现(转载)
    CSS设置元素背景为半透明, 而其中的内容为不透明
    Hibernate的update() 无效
  • 原文地址:https://www.cnblogs.com/ping2yingshi/p/12401693.html
Copyright © 2011-2022 走看看