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 ...
随机推荐
- c#比较器 排序
原地址:http://blog.csdn.net/xutao_ustc/article/details/6314057 class Program { static void Main(string[ ...
- ErrorCode枚举类型返回错误码信息测试,手动抛出异常信息,在事务中根据错误码来回滚事务的思路。
ErrorCode.java 简单测试代码,具体应用思路:手动抛出异常信息,在事务中根据错误码来回滚事务的思路. public enum ErrorCode { //系统级 SUCCESS(" ...
- Codeforces 452E Three Strings(后缀自动机)
上学期很认真地学了一些字符串的常用工具,各种 suffix structre,但是其实对后缀自动机这个部分是理解地不太透彻的,以致于看了师兄A这题的代码后,我完全看不懂,于是乎重新看回一些学习后缀自动 ...
- Java加密技术
相关链接: Java加密技术(一)——BASE64与单向加密算法MD5&SHA&MAC Java加密技术(二)——对称加密DES&AES Java加密技术(三)——PBE算法 ...
- 2014多校第六场 1010 || HDU 4930 Fighting the Landlords (模拟)
题目链接 题意 : 玩斗地主,出一把,只要你这一把对方要不了或者你出这一把之后手里没牌了就算你赢. 思路 : 一开始看了第一段以为要出很多次,实际上只问了第一次你能不能赢或者能不能把牌出尽. #inc ...
- win7系统中任务计划程序的使用与查询
任务计划程序是电脑中的一个好工具,用好了,会让我们使用电脑变的很便捷,具体经验教程如下所示: 工具/原料 装有win7系统的电脑 方法/步骤 在桌面找到“我的电脑”,右击,弹出窗口,找到“管理”,如下 ...
- converntion
One convention that we have is to use the names of fruits and vegetables for variables(only in small ...
- Eclipse项目的导入跟导出
1.导入项目 当下载了包含Eclipse 项目的源代码文件后,我们可以把它导入到当前的Eclipse 工作区然后编辑和查看.点击菜单File > Import,然后在弹出的Import 对话框中 ...
- android+apimonitor+genymotion
1. 安装genymotion: http://www.genymotion.net/ 2. 设置使用adb Setting--adb--选择sdk的目录 3. apimonitor https:// ...
- ubuntu下如何快速加密可移动存储设备
ubuntu下可以快速加密U盘和其他移动存储设备.访问的时候会要你输入密码,这样就比较安全了. ubuntu的磁盘工具使用的是LUKS加密,虽然这个加密方法不跟其他系统兼容,但可以在其他试用GNOME ...