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)
上一篇 2022年1月30日 上午9:00
下一篇 2022年1月30日 上午10:00


相关推荐

  • Gemini冲刺纳斯达克 币圈又迎IPO

    Gemini冲刺纳斯达克 币圈又迎IPO

    2026年3月16日
    3
  • 九款Linux文本编辑器

    九款Linux文本编辑器元老级的工具EmacsKVIM用现代的方式编写代码Arachnophilia  Bluefish:Bluefish标榜其自身是“一款为熟练的Web设计员和程序员而设的编辑器”。KomodoEditNEdit字处理的一些选择Gedit:是LinuxGNOME桌面上一款小巧的文本编辑器。Kate集成的工具QuantaPl

    2022年7月26日
    10
  • Ubuntu解决华硕x550C的WiFi问题

    Ubuntu解决华硕x550C的WiFi问题执行下列命令后 重新启动 echo optionsasus nb wmiwapf 4 sudotee etc modprobe d asus nb wmi conf

    2026年3月26日
    1
  • KVM虚拟机(Centos7)

    KVM虚拟机(Centos7)KVM 虚拟机一 KVM 概述二 虚拟机开启条件三 准备工作完成后的步骤四 KVM 虚拟机管理一 KVM 概述简介 KVM 是 KernelVirtua 的简写 目前 Redhat 只支持在 64 位的 Rhel5 4 以上的系统运行 同时硬件需要支持 VT 技术 KVM 的前身是 QEMU 在 2008 年被 Redhat 公司收购并且获得了一项 hypervisor 技术 不过 redhat 的 KVM 被认为将成为未来 linuxhypervi 的主流 准确的来说 KVM 不仅仅是 linux 内核的一个模块 管理和创建完成的 KV

    2025年11月22日
    6
  • AstrBot 完整安装教程(2026):Docker 部署 + NapCat 接入 QQ,一次搞定

    AstrBot 完整安装教程(2026):Docker 部署 + NapCat 接入 QQ,一次搞定

    2026年3月16日
    3
  • 如何关闭或启动mysql服务器_MySQL运行

    如何关闭或启动mysql服务器_MySQL运行方法一:通过命令行开启\关闭服务第一步点击开始菜单,找到cmd,以管理员身份运行。第二步在命令行里面输入“netstopmysql”,按回车键,就停止了mysql服务,如下图所示:第三步在命令行输入“netstartmysql”,接着按回车键,开启mysql服务,如下图所示:END方法二:通过电脑服务开启/关闭第一步最开始右键计算机,选择“管理”,如下图所示:第二步进去管理之后,选择服务和…

    2025年7月25日
    9

发表回复

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

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