UVa 575 / ZOJ 1712 / Mid-Central USA 1997 Skew Binary (water ver.斜二进制)

2024-03-05 20:32

本文主要是介绍UVa 575 / ZOJ 1712 / Mid-Central USA 1997 Skew Binary (water ver.斜二进制),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

575 - Skew Binary

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=516

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1712

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, 

\begin{displaymath}81307_{10} = 8 \times 10^4 + 1 \times 10^3 + 3 \times 10^2 + ......mes 10^1 +7 \times 10 0 = 80000 + 1000 + 300 + 0 + 7= 81307.\end{displaymath}

When a number is expressed in binary, the k-th digit represents a multiple of 2k. For example, 

\begin{displaymath}10011_2 = 1 \times 2^4 + 0 \times 2^3 + 0 \times 2^2 + 1 \times 2^1 +1 \times 2^0 = 16 + 0 + 0 + 2 + 1 = 19.\end{displaymath}

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, 

\begin{displaymath}10120_{skew} = 1 \times (2^5 - 1) + 0 \times (2^4-1) + 1 \tim......2 \times (2^2-1) + 0 \times (2^1-1)= 31 + 0 + 7 + 6 + 0 = 44.\end{displaymath}

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  2 31  - 1 = 2147483647.

Sample Input 

10120
200000000000000000000000000000
10
1000000000000000000000000000000
11
100
11111000001110000101101102000
0

Sample Output 

44
2147483646
3
2147483647
4
7
1041110737

直接模拟,注意些小技巧。


完整代码:

/*UVa: 0.015s*/
/*POJ: 16ms,164KB*/
/*ZOJ: 0ms,180KB*/#include<cstdio>
#include<cstring>char skew[32];int main(void)
{while (scanf("%s", skew), strcmp(skew, "0")){int len = strlen(skew), ans = 0;for (int i = 0; i < len; i++)ans += (skew[i] & 15) * ((1 << len - i) - 1);printf("%d\n", ans);}return 0;
}


这篇关于UVa 575 / ZOJ 1712 / Mid-Central USA 1997 Skew Binary (water ver.斜二进制)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/777730

相关文章

二进制文件转化成文本文件

文章中如果有写错、表述不明、有疑问或者需要扩展的知识,欢迎留言或者私信~   1.区别 如果一个文件说是文本文件,使用任何一种文本编辑器打开可以展现出人类可读信息字符,因为编码都符合某种编码方式,如ASCII、UTF8、GB2312等等(在文件头可以读出来是什么编码方式,然后文本编辑器再按照规则去读取翻译成对应的字符,展示给我们的就是可读的了)。(关于编码方式不了解可以看这一篇) 如果一

JAVA读取MongoDB中的二进制图片并显示在页面上

1:Jsp页面: <td><img src="${ctx}/mongoImg/show"></td> 2:xml配置: <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001

剑指Offer—编程题10(二进制中1 的个数)

代码如下,请在JDK7及以上版本运行: public class Test10 {/*** 请实现一个函数, 输入一个整数,输出该数二进制表示中1的个数。* 例如把9表示成二进制是1001 ,有2位是1. 因此如果输入9,该出2。** @param n 待的数字* @return 数字中二进制表表的1的数目*/public static int numberOfOne(int

UVA - 12206 Stammering Aliens (hash)

这题其实很容易想到2分长度,关键是2分后,怎么判断出现最多次的串是否》=m次了。这里需要用到hash来处理。 对于一个字符串   H[i] =H[i+1]*131+s[i]  ;其中 H[n]=0;那么对于一个长度为L的串 ,hanh[i,l]=H[i]-H[i+L]*x^L ; 这样记录每个字符串的hash值,然后再处理起来就比较简单了。 VIEW CODE #include<

递归实现十进制转二进制

#include<stdio.h>#include<stdlib.h>//输入一个十进制,自己写一个函数转换为二进制//10进制转2进制:方法是除以2取余,逆序排列 //如果想转换为 8进制,则把代码中的2改为8就OK //下面这个是递归!!! void change2(int num)//如果输入num为10 {if(num==0){return ; }else{/* 不能这样写

java字符串在内存和文件中编码的不同——如何理解进制(二进制)与编码(UTF-8)的关系

不管是在内存中,还是文件中,还是网络传输中,计算机运算和存储的都只能是二进制。 内码是程序内部使用的字符编码,特别是某种语言实现其char或String类型在内存里用的内部编码;外码是程序与外部交互时外部使用的字符编码。 “外部”相对“内部”而言;不是char或String在内存里用的内部编码的地方都可以认为是“外部”。例如,外部可以是序列化之后的char或String,或者外部的文件、命令

二进制炸弹的fp是什么?

🏆本文收录于「Bug调优」专栏,主要记录项目实战过程中的Bug之前因后果及提供真实有效的解决方案,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家关注&&收藏&&订阅!持续更新中,up!up!up!! 问题描述   我在解二进制炸弹第四阶段的递归时,对主函数中的片段的理解如下: 8bc4: e3530002 cmp r3, #28bc8:

C语言笔试题:实现把一个无符号整型数字的二进制序列反序后输出

目录 题目 实例 方法一:直接交换 方法二:间接交换 拓展 题目 编写一个函数,将一个无符号整数的所有位逆序(在32位机器下) 实例 例如有一个无符号整数  unsigned int num = 32; unsigned int 在32位系统中占4个字节(32位) 32的二进制数是:       0000 0000 0000 0000 0000 0000  0010

[leetcode] 107. Binary Tree Level Order Traversal II

Binary Tree Level Order Traversal II 描述 Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root). For example

[leetcode] 102. Binary Tree Level Order Traversal

Binary Tree Level Order Traversal 描述 Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level). For example: Given binary tree [3,9,20