zoukankan      html  css  js  c++  java
  • BUPT OJ84 SINGLE NUMBER

    题目描述

    Given an array with N integers where all elements appear three times except for one. Find out the one which appears only once.

    输入格式

    Several test cases are given, terminated by EOF.

    Each test case consists of two lines. The first line gives the length of array N(1N105), and the other line describes the Nelements. All elements are ranged in [0,2631].

    输出格式

    Output the answer for each test case, one per line.

    输入样例

    4
    1 1 1 3
    10
    1 2 3 1 2 3 1 2 3 4

    输出样例

    3
    4

     

    超时的代码:

    #include <stdio.h>
    #define MAX 100000
    int main()
    {
        int N;
        int a[MAX];
        int i,j;
        int count[MAX];    
        while( scanf("%d",&N)!=EOF)
        {
            
            for(i=0;i<N;i++)
        {
            scanf("%d",&a[i]);
        } 
        for(i=0;i<N;i++)
        {
            count[i]=0;
            for(j=0;j<N;j++)
           {
                  if(a[i]==a[j])
                     count[i]++;
           }
        }       
        for(i=0;i<N;i++)
           if(count[i]==1)
             printf("%d
    ",a[i]);
        } 
        
    }

    网上另类的思路:

    http://blog.csdn.net/birdstorm_s/article/details/19967311

    主要思路是对所有数字进行异或运算, 模拟二进制加法

     因为异或运算有a⊕b⊕a=b这样的性质, 所以可以设app_1, app_2代表出现一次和两次的数, 用not_three代表所有没有出现三次的数, 通过位运算, 最后app_2和出现三次的数被清0, 只剩下app_1, 也就是结果

    有人提到前面解释不清楚,那我更新一下。异或运算本质是二进制位运算,app_1保留出现奇数次的数,一旦出现两次就将进入app_2并从app_1中擦除,如果出现三次就会因为同时出现在app_1和app_2中而被not_three擦除,每次app_1和app_2都要和not_three进行一次与运算确保app_1和app_2中没有存放大于等于三次的数, 所以最终保留的app_1就是只出现一次的数。这个办法通过改变变量个数可以满足一切的查找唯一出现次数的问题,可设app[]进行保存。

    算法复杂度是O(n)

    #include<stdio.h>  
       
    main()  
    {  
        long long x, n, app_1, app_2, not_three;  
        while(scanf("%lld",&n)!=EOF){  
            app_1=0, app_2=0;  
            while(n--){  
                scanf("%lld",&x);  
                app_2 |= app_1&x;  
                app_1 ^= x;  
                not_three = ~(app_1&app_2);  
                app_1 = not_three&app_1;  
                app_2 = not_three&app_2;  
            }  
            printf("%lld
    ",app_1);  
        }  
        return 0;  
    }  
  • 相关阅读:
    吴恩达《深度学习》第四门课(3)目标检测(Object detection)
    cnn知识点汇总
    吴恩达《深度学习》第四门课(2)卷积神经网络:实例探究
    吴恩达《深度学习》第四门课(1)卷积神经网络
    解决Eclipse中springBoot中文乱码问题
    SpringBoot返回页面乱码解决
    【Maven】---Linux搭建Nexus3.X私服
    ASP.NET中TextBox控件设立ReadOnly="true"后台取不到值
    Winform打包工具SetupFactory 9 的使用
    PostgreSQL
  • 原文地址:https://www.cnblogs.com/lipching/p/6402252.html
Copyright © 2011-2022 走看看