UVa 11933 - Splitting Numbers

来源:互联网 发布:sql联合主键怎么设置 编辑:程序博客网 时间:2024/06/06 16:29

題目:給你一個數字n,將2進制中的1按出現的第奇偶次分成兩個數字。

分析:簡單題,位運算。直接統計偶數的1出現的位置加和生成b,n-b即為a。

說明:╮(╯▽╰)╭。

#include <cstring>#include <cstdio>int main(){int n, b, count;while (~scanf("%d",&n) && n) {b = count = 0;for (int i = 0; i < 32; ++ i)if ((1<<i)&n && (count ++)%2)b += 1<<i;printf("%d %d\n",n-b,b);}    return 0;}


0 0