非常好玩的一道题。能够熟悉下位操作实现和玩一玩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 采用https 协议访问接口

    申请好证书后,发现ios 仍无法使用https协议访问到数据,发现ios 需要ssl 支持 TLS1.2 . 更改nginx 配置: ssl_protocols TLSv1 TLSv1. TLSv1. ...

  2. (转)25个增强iOS应用程序性能的提示和技巧--高级篇

    高级当且仅当下面这些技巧能够解决问题的时候,才使用它们: 22.加速启动时间23.使用Autorelease Pool24.缓存图片 — 或者不缓存25.尽量避免Date格式化 高级性能提升 寻找一些 ...

  3. Git的一些用法(建立新的branch)

    建立新的branch和查看全部的branch(kk的代码是基于现有的branch) 切换到branch kk: 当然我们也能够在android studio里操作: 注意切换的时候代码会丢失,必须先c ...

  4. Exchange Server 2010升级到Exchange Server 2013概览

  5. [转]轻量级 Java Web 框架架构设计

    工作闲暇之余,我想设计并开发一款轻量级 Java Web 框架,看看能否取代目前最为流行的而又越来越重的 Spring.Hibernate 等框架.请原谅在下的大胆行为与不自量力,本人不是为了重造轮子 ...

  6. 【问题备注】VS2012不能输入代码,文字…

    第一次遇到,非常奇怪,一个项目,VS2012能正常打开,但是不能输入代码. 对比分析发现,其他项目能正常work.在于一个问题就是,VS2012 右下角有个INS一直在转一直analyzing,而正常 ...

  7. 前端--关于CSS文本

    文本是网页中最重要的一种内容形式,文本几乎可以写在任何地方,块级元素中可以写行内元素中也可以写.文本都是由一个个字符组成的 ,在css布局中,每一个字符都有一个em框,通常font-size设置的大小 ...

  8. Oracle - 找不到原因的无效字符

      当执行Oracle语句时,提示“无效字符”,而语句并无错误时,尝试把语句中的空格替换成半角状态的.   一般直接复制的语句会出现这种问题.

  9. asp.net页面按Enter键IE不提交表单

    //当按下回车键时,让指定的按钮获取指定的文本框的事件                this.txtFNick.Attributes.Add("onkeydown", " ...

  10. 1207--ATM自动取款机的实现

    #include <stdio.h> #include <stdlib.h> #include <stdbool.h> //提示用户操作 void alert(ch ...