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 ...
随机推荐
- ID3决策树---Java
1)熵与信息增益: 2)以下是实现代码: //import java.awt.color.ICC_ColorSpace; import java.io.*; import java.util.Arra ...
- Find the smallest number whose digits multiply to a given number n
Given a number ‘n’, find the smallest number ‘p’ such that if we multiply all digits of ‘p’, we get ...
- idHTTP访问百度
百度屏蔽了indy的客户端标识的 Mozilla/3.0 (compatible; Indy Library),把‘Indy Library’去掉就可以了. try IdHTTP1.Request.U ...
- mysql 存储过程事务
DECLARE t_error INTEGER DEFAULT ; DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=; START TRAN ...
- ElasticSearch使用IK中文分词---安装步骤记录
提示1:必须保证之前的ES中不存在index, 否则ES集群无法启动, 会提示red! 提示2:下载的IK如果太新,会报错 TokenStream被重载Caused by: java.lang.Ver ...
- poj 2311 Cutting Game 博弈论
思路:求SG函数!! 代码如下: #include<iostream> #include<cstdio> #include<cmath> #include<c ...
- Spring MVC 教程,快速入门,深入分析(转)
原文地址:http://elf8848.iteye.com/blog/875830/
- hdu2022(water~~)海选女主角
http://acm.hdu.edu.cn/showproblem.php?pid=2022 二B了,没读题直接错了两次....郁闷 #include <iostream> #includ ...
- Project Euler 104:Pandigital Fibonacci ends 两端为全数字的斐波那契数
Pandigital Fibonacci ends The Fibonacci sequence is defined by the recurrence relation: F[n] = F[n-1 ...
- ActiveMQ和Tomcat的整合应用(转)
转自:http://topmanopensource.iteye.com/blog/1111321 ActiveMQ和Tomcat的整合应用 博客分类: ActiveMQ学习和研究 在Active ...