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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 修改注册表为当前用户和默认用户添加日文输入法

    修改注册表为当前用户和默认用户添加日文输入法修改注册表添加日文输入法步骤1.将日文输入法需要的文件拷贝到目录中2.合并注册表键值为当前用户添加中文拼音输入法和日文输入法3.为默认用户添加日文输入法①加载默认用户注册表②合并下列注册表内容,为默认用户添加输入法③卸载默认用户注册表文件,保存修改的内容。步骤1.将日文输入法需要的文件拷贝到目录中从日文系统中拷贝下列目录中的全部文件到中文系统中的相同目录中。C:\Windows\IME\IMEJP\DICTS拷贝后目录如下图所示。也可以在下面链接中下载相应的文件。日文输入法文件2.合并注册

    2022年10月9日
    2
  • 全网最热Vue入门教程你不看就吃亏了哦[通俗易懂]

      因为最近需要使用到Vue,所以打算将Vue的学习资料详细整理一份,感兴趣的小伙伴可以一起来哦。一、Vue基础介绍1.什么是Vue.jsVue.js是目前最火的一个前端框架,React是最流行的一个前端框架(React除了开发网站,还可以开发手机App,Vue语法也是可以用于进行手机App开发的,需要借助于Weex)Vue.js是前端的主流框架之一,和Angular.js、Rea…

    2022年4月17日
    94
  • Protostuff序列化分析

    Protostuff序列化分析Protostuff序列化分析最近项目中需要将业务对象直接序列化,然后存数据库;考虑到序列化、反序列化的时间以及生产文件的大小觉得Protobuf是一个很好的选择,但是Protobuf有的问题就是需要有一个.proto的描述文件,而且由Protobuf生成的对象用来作为业务对象并不是特别友好,往往业务对象和Protobuf对象存在一个互相转换的过程;考虑到我们仅仅是将业务对象直接序列化到数据库,发现Protobuf在这种情况下并不是特别的好;这时候发现了Protostuff,protostuff不需

    2022年5月6日
    45
  • gamma校正什么意思_伽马校正系数

    gamma校正什么意思_伽马校正系数UNDERSTANDINGGAMMACORRECTIONGammaisanimportantbutseldomunderstoodcharacteristicofvirtuallyalldigitalimagingsystems.Itdefinestherelationshipbetweenapixel’snumericalvalueand

    2022年9月22日
    5
  • Scala_scala定义变量

    Scala_scala定义变量scala中print是直接输出print(s”$a”)输出为变量的值println()打印时自动换行println()相当于print(+’\n’)objectTestScala{defmain(args:Array[String]):Unit={println(“HelloScala!!”)vala1=’T’vararr=…

    2022年9月27日
    5
  • AMQP机制_cdm机制为什么停止了

    AMQP机制_cdm机制为什么停止了当前各种应用大量使用异步消息模型,并随之产生众多消息中间件产品及协议,标准的不一致使应用与中间件之间的耦合限制产品的选择,并增加维护成本。AMQP是一个提供统一消息服务的应用层标准协议,基于此协议的客户端与消息中间件可传递消息,并不受客户端/中间件不同产品,不同开发语言等条件的限制。        当然这种降低耦合的机制是基于与上层产品,语言无关的协议。AMQP协议是一种二进制协议,提供

    2025年6月18日
    3

发表回复

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

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