• [九度][何海涛] 数组中只出现一次的数字


    题目描述:
    一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
    输入:
    每个测试案例包括两行:
    第一行包含一个整数n,表示数组大小。2<=n <= 10^6。
    第二行包含n个整数,表示数组元素,元素均为int。
    输出:
    对应每个测试案例,输出数组中只出现一次的两个数。输出的数字从小到大的顺序。
    样例输入:
    8
    2 4 3 6 3 2 5 5
    样例输出:
    4 6
     1 #include <iostream>
     2 #include <cstdio>
     3 using namespace std;
     4 
     5 int a[1000000];
     6 
     7 int main()
     8 {
     9     int n;
    10     while(scanf("%d", &n) != EOF)
    11     {
    12         int res = 0;
    13         for(int i = 0; i < n; i++)
    14         {
    15             scanf("%d", &a[i]);
    16             res ^= a[i];
    17         }
    18 
    19         int mask = 1;
    20         while((mask & res) == 0)
    21             mask = mask << 1;
    22 
    23         int num1 = 0;
    24         int num2 = 0;
    25         for(int i = 0; i < n; i++)
    26             if ((a[i] & mask) == 0)
    27                 num1 ^= a[i];
    28             else
    29                 num2 ^= a[i];
    30 
    31         if (num1 > num2)
    32         {
    33             int t = num1;
    34             num1 = num2;
    35             num2 = t;
    36         }
    37 
    38         cout << num1 << ' ' << num2 << endl;
    39     }
    40 }
  • 相关阅读:
    机器学习 深度学习 计算机视觉 资料汇总
    激活层和pooling的作用
    NVIDIA GPU 计算能力
    TX2 刷机过程
    Anaconda tensorflow 安装笔记
    yolo-开源数据集coco kitti voc
    TX2上yolov3精度和速度优化方向
    yolo原理学习
    ubuntu常用命令
    tensorflow mnist模块详解
  • 原文地址:https://www.cnblogs.com/chkkch/p/2784098.html
Copyright © 2020-2023  润新知