HOJ 1128 Find Them
来源:互联网 发布:台达plc编程指令大全 编辑:程序博客网 时间:2024/06/03 19:46
HOJ 1128 Find Them
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submit: 741(197 users) Total Accepted: 225(176 users) Rating: Special Judge: NoDescription
给出N个数(保证N为偶数),其中有且只有两个不同的数字出现了奇数次。请你找出他们来。Input
对于每组测试数据:
第一行,给出数字N,表示下一行输入N个数字,其中N为偶数(2<=N<=100000)
第二行,有N个数字(每个数字保证可以使用INT表示)
处理到文件结束
Output
对于每组测试数据:
第一行,输出找到的两个数字,由小到大。
Sample Input
4
1 2 3 3
Sample Output
1 2
先写我自己的一个做法…大佬轻点打..
#include<stdio.h>#include<stdlib.h>#include<string.h>struct node{ char number[6]; int num;}number[100000];int cmp(const void *, const void *);int main(){ int i,t; int n,m,max; while(~scanf("%d",&n)){ getchar(); max = 0; getchar for(i = 0; i < n; i++){ for(t = 0; t < max; t++){ if(number[t].number == m){ number[t].num++; number[t].num %= 2; break; } } if(t == max){ number[t].number = m; number[t].num = 0; max++; } } qsort(number,max,sizeof(number[0]),cmp); printf("%d %d\n",number[0].number,number[1].number); } return 0;}int cmp(const void * a, const void * b){ struct node * c = (struct node *) a; struct node * d = (struct node *) b; if(c->num == d->num) return c->number - d->number; else return c->num - d->num;}
完全是暴力的一种做法 最后超时了
然后搜了一下 发现了一种很奇妙的解法
利用0^a = a, a ^ b = c, a^c = b。用这个特性对数组进行分组
然后分别求出a,b中一个数,最后解出答案
#include<stdio.h>int number[100000];int main(){ int i,j; int n,m; int num1,num2; while(~scanf("%d",&n)){ num1 = 0; for(i = 0; i < n; i++){ scanf("%d",&number[i]); num1 ^= number[i]; } num2 = num1; for(j = 1;; j = j * 2){ if(j & num1) break; } num1 = 0; for(i = 0; i < n; i++){ if(j & number[i]) num1 = num1 ^ number[i]; } num2 = num2 ^ num1; if(num2 < num1){ j = num1; num1 = num2; num2 = j; } printf("%d %d\n",num1,num2); } return 0;}
0 0
- HOJ 1128 Find Them
- Find them, Catch them
- Find them, Catch them
- Find them, Catch them
- Find them, Catch them
- Find them, Catch them
- Find them, Catch them
- Find them, Catch them
- Find them, Catch them
- Find them, Catch them
- Find them, Catch them
- Find them, Catch them
- Find them, Catch them
- pku1703 Find them, Catch them
- PKU1703 find them catch them
- Find them,catch them(P1703)
- poj1703 - Find them, Catch them
- POJ1703 Find them, Catch them
- int类型与char类型的转换
- 前端当然少不了跟颜色打交道
- 题目1020:最小长方形
- Vijos 1451 圆环取数 【区间DP】
- 最大公约数和最小公倍数
- HOJ 1128 Find Them
- 递归方式+FindWindow 遍历窗口句柄。查找已知句柄下的所有句柄。按节点方式保存
- ID3DXConstantTable:: GetConstantByName method 返回的D3DXHANDLE是0x00000 或者发现多纹理混乱
- mongodb 在windows下面配置
- HDUOJ 1466 计算直线的交点数
- spring技术内幕 读书笔记(二)
- 题目1034:寻找大富翁
- html各种宽高
- linux chmod修改文件夹\文件权限