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


相关推荐

  • RT-Thread中自定义 FinSH 命令「建议收藏」

    RT-Thread中自定义 FinSH 命令「建议收藏」在使用RT-Thread中的FinSH命令时,除了系统默认的FinSH命令以外,我们还可以自定义FinSH命令。下面就来演示一下如何自定义FinSH命令。关于FinSH命令的详细用法请参考官方资料https://www.rt-thread.org/document/site/programming-manual/finsh/finsh/。要使用FinSH命令首先要添加FinSH组件,添加组件的详细过程见手把手教你移植RT-Thread系统,FinSH组件添加成后,打开建立…

    2022年5月22日
    56
  • 矩阵的模的平方matlab,matlab求矩阵、向量的模

    矩阵的模的平方matlab,matlab求矩阵、向量的模求矩阵的模:functioncount=juZhenDeMo(a,b)[r,c]=size(a);%求a的行列[r1,c1]=size(b);%求b的行列count=0;forj=1:r-r1+1%所求的行数中取fori=1:c-c1+1%所有的列数中取d=a(j:j+r1-1,i:i+c1-1);e=double(d==b);if(sum(e(:))==r1*c1)…

    2022年5月14日
    54
  • HD2AV_F3B

    HD2AV_F3B文档内容:循环存储器的编写,每一行的像素输入进行存储,再依据目标像素所在行进行相应的读取。工程中会开辟一定空间的RAM用于存储,但是以一个循环的顺序去读写换时间节点:2014/12/20~2014/12/22一、循环RAM循环RAM即为一个循环读写的存储模块,数据填充满存储区间之后再从头接写入覆盖原有的存储空间。文档HD2AV_F3A中…

    2022年7月11日
    22
  • springsession使用_常见的使用null场景

    springsession使用_常见的使用null场景目录一、同域名下相同项目(集群环境)实现Session共享1.思路2.架构图3.实现步骤一、同域名下相同项目(集群环境)实现Session共享在同一个域名下,比如:www.p2p.com同一个项目,部署了多台tomcat,这就是典型的集群。我们上一篇文章的入门案例就属于这种应用场景,只不过在实际开发的过程中,我们如果存在了tomcat集群,那么肯定会使用nginx进行负载均衡,那么这种情况下我们该如何处理。1.思路我们将上一个阶段的p2p项目实现集群部署下的Ses

    2025年6月6日
    2
  • spring整合log4j_log4j和logback同时使用

    spring整合log4j_log4j和logback同时使用常用日志框架log4j、log4j2(log4j的升级版,最常用的)、logback(spring boot默认)、Jboss-logging…等slf4 是日志接口规范,代码对接slf4,实现和具体日志框架解耦,无需修改编码即可切换日志框架。修改pom依赖<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-st

    2022年8月8日
    8
  • Java基础:volatile详解

    Java基础:volatile详解Java基础:volatile1、volatile保证可见性1.1、JMM模型的引入1.2、volatile保证可见性的代码验证1.2.1、无可见性代码验证1.2.1、volatile保证可见性验证2、volatile不保证原子性问:请谈谈你对volatile的理解?答:volatile是Java虚拟机提供的轻量级的同步机制,它有3个特性:1)保证可见性2)不保证原子性3)禁止指令重排刚学完java基础,如果有人问你什么是volatile?它有什么作用的话,相信一定非常懵逼…可能看了答案,也完

    2022年7月18日
    18

发表回复

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

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