非常好玩的一道题。能够熟悉下位操作实现和玩一玩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

1
2
10
21
0

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";
}
}

COJ 1059 - Numeric Parity 位操作的更多相关文章

  1. Codeforces #180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  2. BZOJ 1059 [ZJOI2007]矩阵游戏 (二分图最大匹配)

    1059: [ZJOI2007]矩阵游戏 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 5281  Solved: 2530[Submit][Stat ...

  3. [转]Excel导入异常Cannot get a text value from a numeric cell解决

    原文地址:http://blog.csdn.net/ysughw/article/details/9288307 POI操作Excel时偶尔会出现Cannot get a text value fro ...

  4. 《Entity Framework 6 Recipes》中文翻译系列 (19) -----第三章 查询之使用位操作和多属性连接(join)

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 3-16  过滤中使用位操作 问题 你想在查询的过滤条件中使用位操作. 解决方案 假 ...

  5. 如何获取byte的各个bit值以及常见位操作

    项目中通过信号采集板的数据获取车上仪表盘指示灯的信息,将接收到的数据转成byte后,还要将每一个Byte的各个Bit值分离出来,这样才知道每个bit的值代表的具体信息.这里记录下如何获取byte的各个 ...

  6. php strtotime 在32位操作系统下的限制

    php strtotime 在32位操作系统下的限制 <?php class DateHelper{ /** * 在32位操作系统下,超过 2038-01-19 03:14:07 ,会溢出 * ...

  7. C#按位操作,直接操作INT数据类型的某一位

    /// <summary> /// 根据Int类型的值,返回用1或0(对应True或Flase)填充的数组 /// <remarks>从右侧开始向左索引(0~31)</r ...

  8. sql 关于查询时 出现的 从数据类型 varchar 转换为 numeric 时出错 的解决方法。

    出现这种问题 一般是查询时出现了 varchar 转 numeric 时出了错  或varchar字段运算造成的 解决方法: 让不能转的数不转换就可以了 sql的函数有个isNumeric(参数) 用 ...

  9. 64位操作系统 通过ODP.NET 访问ORACLE 11g

    摘要:64位操作系统部署.NET 程序访问oracle时,无法连接问题.(注意:客户端是64位系统 ,服务端是否64位 还是32位无关.) 1.到oracle 官网搜索相关版本的 ODAC网址: ht ...

随机推荐

  1. ios获取权限

    ios获取权限 by 伍雪颖 -(void)requestRecord{ [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL ...

  2. hdu 4908 BestCoder Sequence

    # include <stdio.h> # include <algorithm> using namespace std; int main() { int n,m,i,su ...

  3. Vs2012于Linux应用程序开发(2):图案

    1.1     代码提示功能 在vs中开发中,Visual Assist是一个很优秀的插件,我们仍然能够使用它进行代码的分析,但它仅仅能支持vcxprojproject,因而我们选择对vcxproj的 ...

  4. nopCommerce架构分析系列(二)数据Cache

    原文(http://www.cnblogs.com/gusixing/archive/2012/04/12/2443799.html)非常感谢作者顾思行的分享! 序言 在很多访问量较大的系统中,尤其在 ...

  5. simplify the life ECMAScript 5(ES5)中bind方法简介

    一直以来对和this有关的东西模糊不清,譬如call.apply等等.这次看到一个和bind有关的笔试题,故记此文以备忘. bind和call以及apply一样,都是可以改变上下文的this指向的.不 ...

  6. 移动前端开发之 viewport 的深入理解

    移动设备上进行网页的重构或开发,首先得搞明白的就是移动设备上的viewport了,只有明白了viewport的概念以及弄清楚了跟viewport有关的meta标签的使用,才能更好地让我们的网页适配或响 ...

  7. HTML5-常见的事件- beforeunload事件

    当我们在日常访问某些网站时,关闭当前网页时出现如下提示: beforeunload 事件就可以完成这样的事情,该事件发生时机:页面卸载之前,可以通过它来取消卸载并继续使用原有页面. 为了显示上面弹出对 ...

  8. python的编码问题研究------使用scrapy体验

    python转码译码 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !impo ...

  9. mysql设置字体

    如果在linux下重启mysql服务的时候出现Job failed to start,在window下重启失败,这是因为你安装了高版本的mysql(mysql5.5以上),在高版本对字符编码方式修改的 ...

  10. TCP 和 UDP 协议发送数据包的大小 (转载)

    MTU最大传输单元,这个最大传输单元实际上和链路层协议有着密切的关系,EthernetII帧的结构DMAC+SMAC+Type+Data+CRC由于以太网传输电气方面的限制,每个以太网帧都有最小的大小 ...