COJ 1059 – Numeric Parity 位操作「建议收藏」

COJ 1059 – Numeric Parity 位操作

大家好,又见面了,我是全栈君。

非常好玩的一道题。能够熟悉下位操作实现和玩一玩bitset这个容器

Description

We define the parity of an integer 
N as the sum of the bits in binary representation computed modulo two. As an example, the number 21 = 10101 has three 1s in its binary representation so it has parity 3 (mod 2), or 1. In this problem you have to calculate the parity of an integer 
1 <= I <= 2147483647 (2^31-1). Then, let start to work…

Input specification

Each line of the input has an integer 
I and the end of the input is indicated by a line where 
I = 0 that should not be processed.

Output specification

For each integer 
I in the input you should print one line in the form ”
The parity of B is P (mod 2).” where 
B is the binary representation of 
I.

Sample input

1210210

Sample output

The parity of 1 is 1 (mod 2).The parity of 10 is 1 (mod 2).The parity of 1010 is 2 (mod 2).The parity of 10101 is 3 (mod 2).

使用bitset来实现。注意bitset的高低为存储顺序,是底位到高位。索引i右0到大的:

void NumericParity()
{
	int n = 0;
	bitset<32> bi;
	while (cin>>n && n)
	{
		bi = n;		
		cout<<"The parity of ";
		bool flag = false;
		for (int i = bi.size() - 1; i >= 0 ; i--)
		{
			flag |= bi.test(i);
			if (flag) cout<<bi[i];
		}		
		cout<<" is "<<bi.count()<<" (mod 2).\n";
	}
}

自家自制的位操作:

static bool biNum[32];

int intTobi(int n)
{
	int i = 0, c = 0;
	while (n)
	{
		c += n % 2;
		biNum[i++] = n % 2;
		n >>= 1;
	}
	return c;
}

void NumericParity2()
{
	int n = 0;
	while (cin>>n && n)
	{
		fill(biNum, biNum+32, false);
		cout<<"The parity of ";
		int c = intTobi(n);
		bool flag = false;
		for (int i = 31; i >= 0 ; i--)
		{
			flag |= biNum[i];
			if (flag) cout<<biNum[i];
		}		
		cout<<" is "<<c<<" (mod 2).\n";
	}
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115818.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • Mysql explain-type使用详解

    Mysql explain-type使用详解下面是测试用例,楼主的测试使用的MySQL版本是5.6.27-log。show createtableuser_man;CREATETABLE`user_man`( `manid`bigint(20)NOTNULLAUTO_INCREMENT, `manname`varchar(32)DEFAULTNULL, `manage`int(6)DEFAULTNULL…

    2022年10月17日
  • 表单提交原理_防止表单重复提交

    表单提交原理_防止表单重复提交1.HTTP是如何提交表单的标签的属性enctype设置以何种编码方式提交表单数据。可选的值有三个:  application/x-www-form-urlencoded:这是默认的编码方式。它只处理表单域里的value属性值,采用这种变法方式的表单会将表单域的值处理成URL方式。  multipart/form-data: 这种编码方式会以二进制流的方式来处理表单数据,这中

    2022年10月6日
  • Windows 7系统安装MySQL5.5.21图解

    Windows 7系统安装MySQL5.5.21图解

    2021年11月16日
  • navicat2021 7 mac激活码【2021免费激活】

    (navicat2021 7 mac激活码)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlMLZPB5EL5Q-eyJsaWN…

    2022年3月20日
  • MOXA串口服务器6450串口引脚图

    大家好,又见面了,我是你们的朋友全栈君。

    2022年4月8日
  • tracert原理及其实现_tracert第一个地址是什么

    tracert原理及其实现_tracert第一个地址是什么Tracert用于测试数据包从发送主机到目的地所经过的网关,它主要用于检查网络连接是否可达,以及分析网络什么地方发生了故障.Tracert的执行过程是:首先发送一个TTL为1的数据包,因此第一跳发送回一个ICMP错误消息以指明此数据包不能被发送(因为TTL超时)之后此数据包被重新发送,TTL为2,同样第二跳返回TTL超时,这个过程不断进行,直到到达目的地.执行这些过程的目的是记录每一个ICM

    2022年9月25日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号