博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
面试题40-------数组中只出现一次的数(数组)
阅读量:4181 次
发布时间:2019-05-26

本文共 371 字,大约阅读时间需要 1 分钟。

题目:一个整型数组里除了两个数字之外,其它的数字都出现了两次。找出这两个数字。

例如数组:{2,4,3,6,3,2,5,5} 返回6和4

代码实现

#include
int s[8]={
2,4,3,6,3,2,5,5};int main(){ int temp = 0; for(int x=0;x<8;x++) temp^=s[x]; temp = temp & (-temp); //第一个为1的位置 int s1 = 0, s2 = 0; for(int x=0;x<8;x++){ //分成两个数组 if(s[x]&temp) s1^= s[x]; else s2^=s[x]; } printf("%d %d",s1,s2);}

结果

这里写图片描述

你可能感兴趣的文章
Service Intent must be explicit
查看>>
android studio SDK开发
查看>>
studio 统计代码的行数
查看>>
字符数组和16进制互换
查看>>
PHP项目中出现致命错误: Class 'Redis' not found
查看>>
There is no tracking information for the current branch.
查看>>
fatal: refusing to merge unrelated histories
查看>>
Git命令还原未提交的变更
查看>>
Linux系统中环境变量的配置
查看>>
Linux系统中配置脚本程序开机启动
查看>>
让Linux系统上的nginx支持php程序
查看>>
源码编译安装LNMP环境之Nginx篇
查看>>
源码编译安装LNMP环境之PHP篇
查看>>
Linux中rpm工具使用教程
查看>>
Linux中yum工具使用教程
查看>>
C++字符串函数
查看>>
mknod详解
查看>>
linux中的run-level何解?
查看>>
Linux内核编译详解(转自linuxSir)
查看>>
实模式,保护模式与V86模式
查看>>