hdu 2095

ps:真是日了狗...英语渣渣理解题目不行,开了个100W数组来算,还优化了下时间,还是超时了,看了题解才知道用异或。

N个数异或,会得出其中是奇数的一个.比如 1^1^3^2^2 = 3.   1^3^1 = 3

先贴上那个开100W数组的题的代码:

#include "stdio.h"
#include "string.h"
int T;
int a[1000100];
int flag[1000100];
int main(){
    int i,j,num;
    while(~scanf("%d",&T) && T ){
        for(i=0;i<T;i++){
            scanf("%d",&a[i]);
            flag[i]=1;
        }
        for(i=0;i<T;i++){
            if(flag[i]==0) continue;
            for(j=0;j<T;j++){
                if(i==j) continue;
                if(a[i]==a[j]){
                    flag[i]=0;
                    flag[j]=0;
                }
            }
        }
        for(i=0;i<T;i++){
            if(flag[i]==1){
                num=i;
                break;
            }
        }
        printf("%d\n",a[num]);
    }
    return 0;
}

然而并不能AC。。。。。。。。

接下来用异或做的能AC。。

代码:

#include "stdio.h"
#include "string.h"
int main(){
    int T,i,k,a;
    while(~scanf("%d",&T) && T){
        for(i=0;i<T;i++){
            scanf("%d",&a);
            if(i==0){
                k=a;
                continue;
            }
            k=k^a;
        }
        printf("%d\n",k);
    }
    return 0;
}
时间: 2024-10-22 14:14:08

hdu 2095的相关文章

hdu 2095 find your present (2) 找到只出现一次的数字

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

hdu 2095 find your present(2)

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

(HDUSTEP 2) hdu 2095 find your present (2)(找到出现奇数次的那个数)

题目如下: find your present (2) Time Limit: 1000/2000 MS (Java/Others) Memory Limit: 32768/1024 K (Java/Others) Total Submission(s): 6275 Accepted Submission(s): 1639   Problem Description In the new year party, everybody will get a "special present"

hdu 2095 find your present (2)

题意:给n个正整数,并保证只有一个数是与众不同的,要你找出与众不同的数. 代码: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int a[1000005]; int main() { int n; while(scanf("%d",&n)&&n) { for(int i=0; i<n; i++) scanf(&qu

hdu 2095 异或“找不同”

经典问题:利用两个相同的数字异或为0的特点,可以用来寻找“落单的数”. 1 #include <cstdio> 2 using namespace std; 3 4 int main () 5 { 6 int n; 7 while ( scanf("%d", &n), n ) 8 { 9 int ans = 0, tmp; 10 for ( int i = 0; i < n; i++ ) 11 { 12 scanf("%d", &t

HDU 2095 find your present(异或)

题意 求一组数中只出现过奇数次的数  输入保证只有一个数满足 知道一个数与自己的异或等于0  与0的异或等于自己就行咯 #include<cstdio> using namespace std; int main() { int n, t, ans; while(scanf("%d", &n), n) { ans = 0; for(int i = 1; i <= n; ++i) { scanf("%d", &t); ans = an

HDU 2095 find your present (2) 异或

异或 暴力开数组,然而明显不过,要求32768k,结果超时了 #include <cstdio> #include <cstring> int book[1000000]; int main() { int n; while (scanf("%d", &n) && n) { int a; memset(book, 0, sizeof(book)); for (int i = 0; i < n; i++) { scanf("

HDU分类

模拟题, 枚举 1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 1049 1050 1057 1062 1063 1064 1070 1073 1075 1082 1083 1084 1088 1106 1107 1113 1117 1119 1128 1129 1144 1148 1157 1161 1170 1172 1177 1197 1200 1201 12

杭电 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