Skew Binary
2015-10-14 20:45
295 查看
题目:
When a number is expressed in decimal, the k-th digit represents a multiple of 10k. (Digits are numbered from right to left, where the least significant digit is number 0.) For example,
When a number is expressed in binary, the k-th digit represents a multiple of 2k. For example,
In skew binary, the k-th digit represents a multiple of 2k+1 - 1. The only possible digits are 0 and 1, except that the least-significant nonzero digit can be a 2. For example,
The first 10 numbers in skew binary are 0, 1, 2, 10, 11, 12, 20, 100, 101, and 102. (Skew binary is useful in some applications because it is possible to add 1 with at most one carry. However, this has nothing to do with the current problem.)
n. If n = 0 it signals the end of the input, and otherwise n is a nonnegative integer in skew binary.
When a number is expressed in decimal, the k-th digit represents a multiple of 10k. (Digits are numbered from right to left, where the least significant digit is number 0.) For example,
When a number is expressed in binary, the k-th digit represents a multiple of 2k. For example,
In skew binary, the k-th digit represents a multiple of 2k+1 - 1. The only possible digits are 0 and 1, except that the least-significant nonzero digit can be a 2. For example,
The first 10 numbers in skew binary are 0, 1, 2, 10, 11, 12, 20, 100, 101, and 102. (Skew binary is useful in some applications because it is possible to add 1 with at most one carry. However, this has nothing to do with the current problem.)
Input
The input file contains one or more lines, each of which contains an integern. If n = 0 it signals the end of the input, and otherwise n is a nonnegative integer in skew binary.
Output
For each number, output the decimal equivalent. The decimal value of n will be at most 231 - 1 = 2147483647.Sample Input
10120 200000000000000000000000000000 10 1000000000000000000000000000000 11 100 11111000001110000101101102000 0
Sample Output
44 2147483646 3 2147483647 4 7 1041110737
代码:
#include<iostream> #include<math.h> #include<string> #include<stack> using namespace std; int main() { string str; cin >> str; while (str != "0") { stack<int> ss; int x; unsigned long long aa = 0; int len = str.length(); for (int i = 0; i < len; i++) { x = (int)(str.at(i) - '0'); // cout << x; aa = aa + x * (pow(2, len - i) - 1); } cout << aa << endl;; cin >> str; } return 0; }
相关文章推荐
- JAVA中的finalize()方法
- centos6.5进入救援模式
- Android中的Fragment详解 ("碎片"这个翻译真是太烂了!)
- 为什么PostgreSQL查询语句也可能产生 xlog, 并且可能对buffer有write操作 ? hint bits
- IOS学习之 响应设备的横竖屏状态,旋转界面 达到横竖屏效果
- jdk环境变量配置以及作用
- Android.mk
- hibernate xml配置
- C++ 头文件cstring,string.h,string 三者的区别
- ARM裸机程序开发(十):C语言环境初始化
- 深入理解Activity1
- 《TCP/IP 详解 卷一》读书笔记 -----第四章 ARP
- HDU 5391 Zball in Tina Town
- yum命令——Linux下只下载不安装
- Android之UI布局
- 仿QQ空间说说TextView内容显示、收起
- 功能测试自动化
- pg_clog的一致性 & 异步事务提交
- java字符流
- UnityVS(2012)安装教程