HDU 2095(map)

来源:互联网 发布:mysql left out join 编辑:程序博客网 时间:2024/06/15 11:37

  这道题刚开始用排序做,结果Memory Limit Exceeded了,发现内存限制在1024k,额,,好吧。。最后看书用map映照容器做的

注意划线部分,不注意很可能会理解错题意!!

               find your present (2)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/1024 K (Java/Others)
Total Submission(s): 14705    Accepted Submission(s): 5568


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
51 1 3 2 231 2 10


 

Sample Output
32
Hint
Hint
use scanf to avoid Time Limit Exceeded

程序如下:

#include<map>#include<cstdio>using namespace std;int main(){    int t,n,i;    while(~scanf("%d",&t),t)    {        map<int,int> m;        for(i=0; i<t; i++)        {            scanf("%d",&n);            m[n]++; //计算每个数字出现的次数        }        map<int,int>::iterator it;        for(it=m.begin(); it!=m.end(); it++)        {            if((*it).second == 1) //如果次数出现1的就是它了            {                printf("%d\n",(*it).first);                break;            }        }    }    return 0;}

 

后来发现有人用异或做出来了,真是叼炸天了。。

程序如下:

//异或^运算符使用规则://先转换为二进制,然后:遵循a^a=0,a^0=a规则#include <stdio.h>int main(){int n,s,a;while(~scanf("%d",&n),n)    {s = 0;while(n--){scanf("%d",&a);s ^= a;}printf("%d\n",s);}return 0;}//如果多次进行^运算,那么出现偶数次的相同的数就会抵消为0,奇数次的数则会保留一个下来//如果还不懂可以自己举个例子,比如①^②^③(代表3个不同的十进制数)如下图://当然奇数次的数只能有一个才能出现正确结果,由于题目是输出奇数次的数,所以可行


 

0 0
原创粉丝点击