UVa 575 Skew Binary 歪斜二进制

呵呵,这个翻译还是很直白的嘛,大家意会就好。

第一次看到这个高大上题目还是有点小害怕的,还好题没有做过深的文章。

只要按照规则转化成十进制就好了,而且题目本身也说了最大不超过一个int的范围(2^31-1 == 2147483647)。

直接位运算就好了。

  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 integer n. 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

Miguel A. Revilla 
1998-03-10

AC代码:

 1 //#define LOCAL
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6
 7 char SkBinary[35];
 8
 9 void Reverse(char s[], int l);
10
11 int main(void)
12 {
13     #ifdef LOCAL
14         freopen("575in.txt", "r", stdin);
15     #endif
16
17     while(gets(SkBinary) && SkBinary[0] != ‘0‘)
18     {
19         int l = strlen(SkBinary);
20         Reverse(SkBinary, l);
21         int i, n = 0;
22         for(i = 0; i < l; ++i)
23         {
24             if(SkBinary[i] == ‘0‘)
25                 continue;
26             if(SkBinary[i] == ‘1‘)
27                 n += (1 << (i + 1)) - 1;
28             if(SkBinary[i] == ‘2‘)
29                 n += (1 << (i + 2)) - 2;
30         }
31         cout << n << endl;
32     }
33     return 0;
34 }
35 //用来反转数组
36 void Reverse(char s[], int l)
37 {
38     int i;
39     char t;
40     for(i = 0; i < l / 2; ++i)
41     {
42         t = s[i];
43         s[i] = s[l - i -1];
44         s[l - i -1] = t;
45     }
46 }

代码君

UVa 575 Skew Binary 歪斜二进制

时间: 2024-10-07 20:48:49

UVa 575 Skew Binary 歪斜二进制的相关文章

uva 575 Skew Binary(数论)

uva 575 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-

UVA 575 Skew Binary (水)

题意:根据这种进制的算法,例如,给你一个左式,要求推出右式.(其实右式就是一个十进制数,根据这种进位的方法来转成特殊进制的数.) 思路:观察转换特点,有点类似于二进制,但是其在后面还减一了.比如25-1.24-1 ...21-1.如果我们不减1会怎样?如上式,从左边看起,就会多加了(25-1)*1+1*(23-1)+2*(22-1).这就是说,我们可以先将 10120 从右往左逐个乘以21  22  23  24  25 再减去10120即可得出结果. 当然也可以按照上边式子老实计算. #inc

寒假集训.Skew Binary

Skew Binary Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 575 Description When a number is expressed in decimal, the k-th digit represents a multiple of 10k. (Digits are numbered from right to left,

【LeetCode-面试算法经典-Java实现】【067-Add Binary(二进制加法)】

[067-Add Binary(二进制加法)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100" 题目大意 给定两个二进制的字符串,返回它们的和,也是二进行制字符串. 解题思路 先将对应的两个二进制字符串

uva 10304 Optimal Binary Search Tree (区间DP)

uva 10304 Optimal Binary Search Tree 题目大意:给出N个结点(已知每个结点的权值)来建树,建树时要满足以下规则:左子树的节点的值要全小于父节点,右子树的节点的值要全大于父节点.要求最后建出的树总权值最小.总权值=各结点乘以层数(从0层开始)之后相加的和. 解题思路:dp[i][j]代表区间第i个结点到第j个结点组成的树最小的总权值.dp[j][i]=min(dp[j][i],dp[j][k?1]+dp[k+1][i]+sum[i]?sum[j?1]?num[k

UVA - 12041 BFS (Binary Fibonacci String)

Description Problem B - BFS (Binary Fibonacci String) We are familiar with the Fibonacci sequence (1, 1, 2, 3, 5, 8, ...). What if we define a similar sequence for strings? Sounds interesting? Let's see. We define the follwing sequence: BFS(0) = 0 BF

【UVA】11464-Even Parity(二进制枚举子集)

枚举第一行的所有可能情况,之后根据上面行计算下面行(判断是否冲突),获得最终结果. 14058243 11464 Even Parity Accepted C++ 0.275 2014-08-18 05:14:15 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #include<stack> #inc

LeetCode OJ:Add Binary(二进制相加)

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 简单的二进制相加而已,只不过传入的参数是字符串而已.为了方便,先将string  reverse了一下,代码如下: 1 class Solution { 2 public: 3 string addBinary(string a, s

LeetCode 67 Add Binary(二进制相加)(*)

翻译 给定两个二进制字符串,返回它们的和(也是二进制字符串). 比如, a = "11" b = "1" 返回 "100". 原文 Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 分析 我一開始写了这个算法,尽管实现