POJ 3191 The Moronic Cowmpouter
来源:互联网 发布:unix环境高级编程源码 编辑:程序博客网 时间:2024/06/06 05:37
Inexperienced in the digital arts, the cows tried to build a calculating engine (yes, it's a cowmpouter) using binary numbers (base 2) but instead built one based on base negative 2! They were quite pleased since numbers expressed in base −2 do not have a sign bit.
You know number bases have place values that start at 1 (base to the 0 power) and proceed right-to-left to base^1, base^2, and so on. In base −2, the place values are 1, −2, 4, −8, 16, −32, ... (reading from right to left). Thus, counting from 1 goes like this: 1, 110, 111, 100, 101, 11010, 11011, 11000, 11001, and so on.
Eerily, negative numbers are also represented with 1's and 0's but no sign. Consider counting from −1 downward: 11, 10, 1101, 1100, 1111, and so on.
Please help the cows convert ordinary decimal integers (range -2,000,000,000..2,000,000,000) to their counterpart representation in base −2.
You know number bases have place values that start at 1 (base to the 0 power) and proceed right-to-left to base^1, base^2, and so on. In base −2, the place values are 1, −2, 4, −8, 16, −32, ... (reading from right to left). Thus, counting from 1 goes like this: 1, 110, 111, 100, 101, 11010, 11011, 11000, 11001, and so on.
Eerily, negative numbers are also represented with 1's and 0's but no sign. Consider counting from −1 downward: 11, 10, 1101, 1100, 1111, and so on.
Please help the cows convert ordinary decimal integers (range -2,000,000,000..2,000,000,000) to their counterpart representation in base −2.
Input
Line 1: A single integer to be converted to base −2
Output
Line 1: A single integer with no leading zeroes that is the input integer converted to base −2. The value 0 is expressed as 0, with exactly one 0.
Sample Input
-13
Sample Output
110111
Hint
Explanation of the sample:
Reading from right-to-left:
Reading from right-to-left:
1*1 + 1*-2 + 1*4 + 0*-8 +1*16 + 1*-32 = -13
说白了 给一个数的负二进制 求出负二进制
#include "iostream"#include "cstring"#include "cstdio"#define N 100000using namespace std;int n;int sk[N],tp;int main(){ scanf("%d",&n); while(n) { int k=n%-2; int t=n/-2; if(k<0) { k+=2; t++; } n=t; sk[++tp]=k; } for(int i=tp;i>=1;i--) printf("%d",sk[i]); if(tp==0) printf("0"); printf("\n"); return 0;}
0 0
- POJ 3191 The Moronic Cowmpouter
- poj 3191 The Moronic Cowmpouter
- POJ 3191 The Moronic Cowmpouter
- POJ 3191The Moronic Cowmpouter
- [POJ 3191 The Moronic Cowmpouter]数学
- POJ 3191 The Moronic Cowmpouter 可能会
- POJ 3191 The Moronic Cowmpouter 笔记
- PKU 3191 The Moronic Cowmpouter
- pku 3191 The Moronic Cowmpouter
- POJ 3191 The Moronic Cowmpouter 负二进制转换
- poj 3191 The Moronic Cowmpouter 十进制转-2进制
- poj 3191The Moronic Cowmpouter (负进位制)
- Poj 3191 The Moronic Cowmpouter【十进制转负二进制】
- POJ 3191 The Moronic Cowmpouter (-2进制转换)
- Poj 3191 The Moronic Cowmpouter【十进制转负二进制】
- POJ3191-The Moronic Cowmpouter
- POJ3191- The Moronic Cowmpouter 十进制转负n进制
- The Moronic Cowmpouter poj3191(负进制转换以及其他进制转换模板)
- CentOS 7 yum安装 Mono 和 手动 安装Jexus
- C++入门经典 笔记 (第十六章)使用继承扩展类
- Android proguard 详解(二)
- leetcode: (226) Invert Binary Tree
- 大二暑假
- POJ 3191 The Moronic Cowmpouter
- Cocos2dx_可擦除Layer
- voyage 虚拟机安装过程
- lombok 介绍及基本使用方法
- java Map 按value值排序和按key值排序
- Panopticlick揭示你不能删除cookie
- Texturepacket命令行工具
- 6to4隧道
- 解析nginx负载均衡