zoukankan      html  css  js  c++  java
  • find your present (2) 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
     
    Sample Output
    3 2
    Hint
    Hint
    use scanf to avoid Time Limit Exceeded
     
    Author
    8600
     
    Source
     
    Recommend
    8600   |   We have carefully selected several similar problems for you:  2094 1008 1597 1593 1595 
     
     
     
     
     
     
     1 //map
     2 #include <set>
     3 #include <stdio.h>
     4 using namespace std;
     5 int main()
     6 {
     7     int n,x;
     8     set <int> S;
     9     while(scanf("%d",&n),n)
    10     {
    11         while(n--)
    12         {
    13             scanf("%d",&x);
    14             if(S.find(x) == S.end())    //没找到,插入
    15                 S.insert(x);
    16             else                        //找到了,删除
    17                 S.erase(x);
    18         }
    19         printf("%d
    ",*S.begin());
    20         S.clear();
    21     }
    22     return 0;
    23 }
     1 //排序
     2 
     3 #include <cstdio>
     4 #include <algorithm>
     5 #include <iostream>
     6 #include <cstdlib>
     7 using namespace std;
     8 
     9 long long int a[1000010]; 
    10 
    11 int main()
    12 {
    13     int n;
    14     while (scanf("%d",&n) && n)
    15     {
    16         for (int i = 0; i<n; i++)
    17             scanf("%lld",&a[i]);
    18         sort(a, a+n);
    19         int i;
    20         for (i = 1; i<n; i++)
    21         {
    22             if (a[i] != a[i-1]  &&  a[i] != a[i+1])
    23                 break;
    24         }
    25         printf("%lld
    ",a[i]);
    26     }
    27     return 0;
    28 }
     1 #include <math.h>
     2 #include <string.h>
     3 #include <algorithm>
     4 #include <stdio.h>
     5 long a[100000100];
     6 int main()
     7 {
     8 
     9     memset(a,0,sizeof(a));
    10     long n;
    11     while(scanf("%ld",&n)!=EOF&&n!=0)
    12     {
    13         long long m;
    14     for(int i=0;i<n;i++)
    15         {
    16             scanf("%lld",&m);
    17             a[m]+=1;
    18         }
    19     for(int i=0;i<100000100;i++)
    20     {
    21        if(a[i]==1)
    22        {
    23              printf("%lld
    ",i);
    24        }
    25     }     
    26     }
    27     return 0;
    28 }

    其实,这题还有个更好的方法————位异或。

    我们先了解一下位异或的运算法则吧:

    1、a^b = b^a。

    2、(a^b)^c = a^(b^c)。

    3、a^b^a = b。

    对于一个任意一个数n,它有几个特殊的性质:

    1、0^n = n。

    2、n^n = 0。

    所以可以通过每次异或运算,最后剩下的值就是出现奇数次的那个数字。

     1 #include <stdio.h>
     2 int main()
     3 {
     4     int n,x,ans;
     5     while(scanf("%d",&n),n)
     6     {
     7         ans = 0;
     8         while(n--)
     9         {
    10             scanf("%d",&x);
    11             ans ^= x;
    12         }
    13         printf("%d
    ",ans);
    14     }
    15     return 0;
    16 }
  • 相关阅读:
    深入A标签点击触发事件而不跳转的详解
    js、css、html判断浏览器的各种版本
    深入理解this对象
    背景透明文字不透明的最佳方法兼容IE(以背景黑色透明度0.5为例)
    解决ie6支持最大高度最小高度的方法
    js点击更多显示更多内容效果
    artdialog关闭弹出窗口
    (巧用)事件代理
    CSS3盒模型display:-webkit-box;的使用
    文件上传input type="file"样式美化
  • 原文地址:https://www.cnblogs.com/wangmengmeng/p/4636473.html
Copyright © 2011-2022 走看看