【蓝桥杯基础算法】位运算例题详解(C++代码)

常用的六种位运算符

& 按位与~取反
按位或<<左移
^ 按位异或>>右移

在这里插入图片描述
在这里插入图片描述

1、找出唯一成对的数

在这里插入图片描述

rand()函数如果未设随机数种子,rand()在调用时会自动设随机数种子为 1,导致每次执行时是都是与上一次相同的。

若要产生每次不同的随机数,则可以使用srand( seed )函数进行产生随机化种子,随着seed的不同,就能够产生 不同的随机数。

srand函数需要用到头文件include<stdlib.h>

#include<iostream>
#include<stdlib.h>
#include <time.h>
using namespace std;
int main()
{
	int arr[1001];
	int length = sizeof(arr) /sizeof(int);
	for (int i = 0; i < length; i++)
		arr[i] = i + 1;
	srand(time(0));	//设置随机种子
	arr[length - 1] = 1 + rand() % (length - 1); //最后一位数随机生成
	for (int i = 0; i < length; i++)
		cout << arr[i]<<" ";
	cout << endl;
	int x = 0;
	for (int i = 1; i <= length - 1; i++)
		x = (x^i); //任何数和0做异或运算结果都为它本身
	for (int i = 0; i < length; i++)
		x = x ^ arr[i];
	cout << x; //得到结果
	return 0;
}

2、找出落单的那个数

在这里插入图片描述

同样利用异或运算,相同的数就会被抵消为0,而与0做异或运算还是它本身。

#include <iostream>
using namespace std;
int main()
{
	int arr[] = { 0,0,5,5,3,7,7 };
	int x = 0;
	int length = sizeof(arr) / sizeof(int);
	for (int i = 0; i < length; i++)
		x = x ^ arr[i];
	cout << x;
	return 0;
}

3、二进制中1的个数

在这里插入图片描述

方法1:该整数不动,1左移

用数字1从右往左移,循环做与运算&,比对每一位,因为相同则说明有1,然后记录次数。结果即为1的个数。

程序使用了bitset转换二进制,需要引入头文件**#include**

方法2:该整数右移,1不动

反向思考方法1

方法3:减1,再做与运算

一直循环减1,做与运算,循环的次数即为1的个数

……1001$- 1 1 1=KaTeX parse error: Expected 'EOF', got '&' at position 15: ……1000,……1001 &̲ ……1000=$……1000;

……1000$- 1 1 1=KaTeX parse error: Expected 'EOF', got '&' at position 15: ……0111,……1000 &̲ ……0111=$0;

自己动手写一下就会发现此法巧妙之处。

#include<iostream>
#include<bitset>
using namespace std;
int main()
{
	int x,count=0;
	cin >> x;
	cout << bitset<sizeof(int) * 8>(x) << endl;//展示二进制数
	//方法1
	for (int i = 0; i < 32; i++)
		if ((x&(1 << i)) == (1 << i))
			count++;
	cout << count<<endl;
	//方法2
	count = 0;
	for (int i = 0; i < 32; i++)
		if ((1 & (x >> i)) == 1)
			count++;
	cout << count << endl;
	//方法3
	count = 0;
	while (x != 0)
	{
		x = (x - 1)&x;
		count++;
	}
	cout << count << endl;
	return 0;
}

4、是不是2的整数次方

在这里插入图片描述

分析2的整数次方,的二进制的特点。可以发现,只在某一位为1其余为为0

这不,与题3的第三种方法不谋而合,十分切合。

#include<iostream>
using namespace std;
int main()
{
	int x = 0;
	cin >> x;
	if(((x-1)&x)==0)
		cout<<"yes";
	else
		cout<<"no";
	return 0;
}

5、将整数的奇偶位互换

看网上的解法,对奇偶的定义有不同的定法。其实把握一个关键:同0xaaaaaaaa做与运算的一定要右移,同0x55555555做与运算的一定要左移,最终结果是一样的。

该代码最左边为起始数,开始数奇偶!

  • 0xaaaaaaaa是10101010……的16进制表示方式
  • 0x55555555是01010101……的16进制表示方式
#include<iostream>
using namespace std;
int main()
{
	int x = 0;
	cin >> x;
	int ji = x & 0xaaaaaaaa;//将数字和10101010.....与运算&得到奇数位
	int ou = x & 0x55555555;//将数字和01010101.....与运算&得到偶数位
	//将奇数位右移一位,偶数位左移一位,然后做异或运算^
	cout << ((ji >> 1) ^ (ou << 1));
	return 0;
}

6、0~1间浮点实数的二进制表示

在这里插入图片描述

乘2取整:将该浮点数乘2得到一个新的数,判断

如果≥1,就在0.后拼接上1,然后减1,反之拼接0,一直循环直到数为0。

#include<iostream>
#include<string>
using namespace std;
int main()
{
	double x = 0;
	cin >> x;
	string s = "0.";
	while (x != 0)
	{
		x *= 2;
		if (x >= 1)
		{
			s += "1";
			x -= 1;
		}
		else
			s += "0";
	}
	//32位以内+上“0.”,所以长度要大于34
	if (s.length() >= 34)
		cout << "ERROR";
	else
		cout << s;
	return 0;
}
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值