您的位置:首页 > 其它

【杭电-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
0
Sample Output
3
2
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
0
Sample 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;} 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: