【杭电-oj】-2095-find your present (2)(输出奇数个的数)
2016-03-26 19:43
471 查看
Problem DescriptionIn 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 numberwill 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 isthe number that different from all the others.InputThe 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.OutputFor 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 0Sample Output
3 2Problem DescriptionIn 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 numberwill 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 isthe number that different from all the others.InputThe 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.OutputFor 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 0Sample Output
3 2
#include<stdio.h> #include<algorithm> using namespace std; bool cmp(int a,int b) { return a>b; } int a[10000000]; int main() { int n,b; while(~scanf("%d",&n)&&n) { for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } sort(a+1,a+n+1,cmp); //先把n个数从大到小排序 b=1; for(int j=1;j<=n;j++) { if(a[j]==a[j+1]) { b=!b; //将数值全部排序后,从前到后依次判断是否相等,直到找到奇数个的数 } else { if(b==1) // 可写为if(b) { printf("%d\n",a[j]); } else //因上一次判断使得b=0,所以将b变为1后再次判断,输出即可 { b=1; } } } } return 0; }以下是两种超时的写法,但是方法应学习!!!!
#include<stdio.h>#include<string.h>int a[10000000];int main(){int n;while(~scanf("%d",&n)&&n){memset(a,0,sizeof(a)); //数组a各值进行初始化为 0for(int i=1;i<=n;i++){int t;scanf("%d",&t);a[t]++; //输入的数值即为a数组的下标,如果有多次便自加一}for(int i=1;i<=n;i++){if(a[i]%2!=0) //从1开始向后搜索,找到奇数个,即与2取余不为 0{printf("%d\n",a[i]);break;}}}return 0;}
</pre><pre code_snippet_id="1624844" snippet_file_name="blog_20160326_3_2065515" name="code" class="cpp">
#include<stdio.h>#include<string.h>int a[10000000];int b[10000000];int main(){int n;while(~scanf("%d",&n)&&n){memset(b,0,sizeof(b)); //对数组b的值进行初始化为 0for(int i=1;i<=n;i++){scanf("%d",&a[i]);}for(int i=1;i<=n;i++){b[a[i]]++; //a[i]的值作为b[]的下标,如果a[i]多次出现即b数组多次自加一}for(int i=1;i<=n;i++){if(b[i]%2==1){printf("%d\n",a[i]);break;}}}return 0;}
相关文章推荐
- MyEclipse与Mysql数据库的连接
- Hive的三种Join方式
- JS随机数种子
- Android RecyclerView 使用完全解析
- 两个大数相乘JAVA版
- [uwp]自定义图形裁切控件
- Java中关于继承与多态的一道选择题
- vi编辑器常用语法
- DES加解密算法的实现
- 存储过程insert into select
- MATLAB以MEX方式调用C代码
- UVA 11214(p217)----Guarding the Chessboard
- 15电气郄慧敏vb作业6比较两个数的大小
- 乘法逆元
- 学习进度表
- 单例模式优缺点
- 【杭电-oj】-1064-Financial Management(财务管理)
- CodeForces - 581B Luxurious Houses
- 函数模板
- 修改Esxi克隆的CentOS的IP地址