杭电 2095

find your present (2)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/1024 K (Java/Others)

Total Submission(s): 15295    Accepted Submission(s): 5797

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

HDU 2007-Spring Programming Contest - Warm Up (1)

这道题乍一看是用数组,但是要是用数组的话,因为题上的数很大 编译器会崩溃,想我一开始就是用的数组就是编辑器崩溃了,后来正好看到了位运算,想到或许可以用位运算优化,要是用异或的话,那么相同的数运算运算就是0,那么那个剩下的唯一的奇数,(实际上在这里就不用在判断是不是奇数了),就是要求的,于是又一下代码:

<span style="font-size:18px;">#include<stdio.h>
int main()
{
	int n,x,m;
	while(~scanf("%d",&n),n)
	{
		x=0;
		while(n--)
		{
			scanf("%d",&m);
			x^=m;
		}
		printf("%d\n",x);
	}
	return 0;
}</span>
<span style="font-size:18px;"></span> 
<span style="font-size:18px;">下面是之前我的错误的代码:</span>
<span style="font-size:18px;"></span><p>#include<stdio.h>
#include<string.h>
int a[1000001];
int s[1000001];
int b[1000001];
int main()
{
 int n,i;</p><p> while(~scanf("%d",&n),n)
 {
  memset(s,0,sizeof(s));
  for(i=1;i<=n;i++)
  scanf("%d",&a[i]);
  for(i=1;i<=n;i++)
  {
   s[a[i]]++;
   b[i]=i;  
  }
  for(i=1;i<=n;i++)
  {
   if(s[a[i]]&1)
   {
    printf("%d\n",b[i]);
   }
  }
 }
 return 0;
}
</p>
<span style="font-size:18px;">//能够运行,但是因为数组设的过大,导致编译器崩溃没法通过。</span>

杭电 2095

时间: 2024-10-10 01:31:38

杭电 2095的相关文章

与众不同 【杭电-2095】 附题

/* find your present (2) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/1024 K (Java/Others) Total Submission(s): 15255    Accepted Submission(s): 5780 Problem Description In the new year party, everybody will get a "special present&qu

杭电 2095 find your present (2)【位运算 异或】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2095 解题思路:因为只有我们要求的那个数出现的次数为奇数,所以可以用位运算来做,两次异或同一个数最后结果不变,那么就可以知道异或运算相当于过滤掉了出现次数为偶数的数,最后只留下了唯一的那一个出现次数为奇数的数. 反思:位运算好陌生,好好学. #include<stdio.h> int main() { int n; long int a; while(scanf("%d",&a

异或^符号在C/C++中的使用 &amp; 杭电oj 2095

异或^符号,在平时的学习时可能遇到的不多,不过有时使用得当可以发挥意想不到的结果. 值得注意的是,异或运算是建立在二进制基础上的,所有运算过程都是按位异或(即相同为0,不同为1,也称模二加),得到最终结果. 特点:任何数和0异或都等于它本身;两个相同的数异或后的结果是0: 举例如下: int a = 4 =100(二进制) int b = 3 =011(二进制) int c = a^b = 111 = 7: 下面就^常用应用做个介绍: 1. 在一排数中找到独一无二的一个数 本例启发来自于杭电oj

杭电acm 2095 find your present (2)

#include<iostream>using namespace std;int main(){    int n,x,y;    while(cin>>n,n)    {         cin>>x;                   while(--n)         {             cin>>y;             x=x^y;         }         cout<<x<<"\n&q

杭电ACM题目分类

杭电ACM题目分类 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028. 1029.1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092. 1093.1094.1095.1096.1097.1098.1106.1108.1157.1163.1164.1170.1194.1196. 1197.1201.1202.1205.1219.1234.123

杭电 HDU 1164 Eddy&#39;s research I

Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7117    Accepted Submission(s): 4268 Problem Description Eddy's interest is very extensive, recently  he is interested in prime

hdu 1016 Prime Ring Problem DFS解法 纪念我在杭电的第一百题

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29577    Accepted Submission(s): 13188 Problem Description A ring is compose of n circles as shown in diagram. Put natural num

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

一个人的旅行 HDU杭电2066【dijkstra算法】

http://acm.hdu.edu.cn/showproblem.php?pid=2066 Problem Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还可以看美丽的风景--草儿想去很多地方,她想要去东京铁塔看夜景,去威尼斯看电影,去阳明山上看海芋,去纽约纯粹看雪景,去巴黎喝咖啡写信,去北京探望孟姜女--眼看寒假就快到了,这么一大段时间,可不