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代码:
- //#define LOCAL
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- using namespace std;
- char SkBinary[];
- void Reverse(char s[], int l);
- int main(void)
- {
- #ifdef LOCAL
- freopen("575in.txt", "r", stdin);
- #endif
- while(gets(SkBinary) && SkBinary[] != '')
- {
- int l = strlen(SkBinary);
- Reverse(SkBinary, l);
- int i, n = ;
- for(i = ; i < l; ++i)
- {
- if(SkBinary[i] == '')
- continue;
- if(SkBinary[i] == '')
- n += ( << (i + )) - ;
- if(SkBinary[i] == '')
- n += ( << (i + )) - ;
- }
- cout << n << endl;
- }
- return ;
- }
- //用来反转数组
- void Reverse(char s[], int l)
- {
- int i;
- char t;
- for(i = ; i < l / ; ++i)
- {
- t = s[i];
- s[i] = s[l - i -];
- s[l - i -] = t;
- }
- }
代码君
UVa 575 Skew Binary 歪斜二进制的更多相关文章
- UVA 575 Skew Binary (水)
题意:根据这种进制的算法,例如,给你一个左式,要求推出右式.(其实右式就是一个十进制数,根据这种进位的方法来转成特殊进制的数.) 思路:观察转换特点,有点类似于二进制,但是其在后面还减一了.比如25- ...
- poj1565---(数论)skew binary
/*将数字存储在数组中 #math.h strlen(a)=len sum=0 for(i=0;i<len;i++) sum+=a[i]*(pow(2,len-i)-1)*/ #include ...
- UVA.11806 Cheerleaders (组合数学 容斥原理 二进制枚举)
UVA.11806 Cheerleaders (组合数学 容斥原理 二进制枚举) 题意分析 给出n*m的矩形格子,给出k个点,每个格子里面可以放一个点.现在要求格子的最外围一圈的每行每列,至少要放一个 ...
- 【LeetCode-面试算法经典-Java实现】【067-Add Binary(二进制加法)】
[067-Add Binary(二进制加法)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given two binary strings, return thei ...
- POJ 1565:Skew Binary
Skew Binary Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10676 Accepted: 6807 Desc ...
- POJ 1565 Skew Binary(简单的问题)
[简要题意]:第二个是数字系统的代表性的定义.而给了你这个号码系统提示的形式和十进制转换之间的关系.现在给你一些这样的系统.让你把它变成2二进制输出. [分析]:当中 base[k] = 2^(k ...
- [LeetCode] Prime Number of Set Bits in Binary Representation 二进制表示中的非零位个数为质数
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...
- [LeetCode] Binary Gap 二进制间隙
Given a positive integer N, find and return the longest distance between two consecutive 1's in the ...
- 762. Prime Number of Set Bits in Binary Representation二进制中有质数个1的数量
[抄题]: Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a ...
随机推荐
- register_globals
register_globals参数为On的时候很危险 这里记录一下各版本register_globals的情况 PHP5.2版本register_globals默认为On PHP5.3 PHP5.3 ...
- 单例模式及C++实现
单例模式及C++实现代码 C++中的单例模式http://blog.csdn.net/hackbuteer1/article/details/7460019
- (转)Eclipse平台技术概述
转载:周金根 http://zhoujg.blog.51cto.com/1281471/516833 Eclipse:Eclipse平台技术概述 2010-10-19 13:35:00 标签:E ...
- ZOJ3229 Shoot the Bullet(有源汇的上下界最大流)
#pragma warning(disable:4996) #include <iostream> #include <cstring> #include <string ...
- HDU2829 Lawrence(斜率优化dp)
学了模板题之后上网搜下斜率优化dp的题目,然后就看到这道题,知道是斜率dp之后有思路就可以自己做不出来,要是不事先知道的话那就说不定了. 题意:给你n个数,一开始n个数相邻的数之间是被东西连着的,对于 ...
- Javascript 正则表达式_3
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 修改DevExpress中英文提示,将英文改为中文
1 : 修改DX 提示框中的英文字符 /// <summary> /// 重写DX弹出框 英文变为中文 /// </summary> public class CHS : De ...
- Andoid自动判断输入是电话,网址或者Email的方法----Linkify的应用!
本节要讲的是,当我们在一个EditText输入电话或者网址还是Email的时候,让Android自动判断,当我们输入的是电话,我们点击输入内容将调用打电话程序,当我们输入是网址点击将打开浏览器程序.而 ...
- 关于android的分辨率
关于Android的分辨率支持,为大家翻译官方文档 看世界杯的空闲时间,翻译一下官方文档.分辨率问题是大家都很关心的(720×480会不会悲剧),而关于这个问题,android官方的文档无疑最有说服力 ...
- [你必须知道的.NET]第二十九回:.NET十年(上)
发布日期:2009.05.08 作者:Anytao © 2009 Anytao.com ,Anytao原创作品,转贴请注明作者和出处. /// <summary> /// 本文部分内容,已 ...