Base -2 - UVa 11121 -2进制
来源:互联网 发布:台湾综艺节目软件 编辑:程序博客网 时间:2024/06/12 19:53
Base -2
Input: Standard Input
Output: Standard Output
The creator of the universe works in mysterious ways. But
he uses a base ten counting system and likes round numbers.
Scott Adams
Everyone knows about base-2 (binary) integers and base-10 (decimal) integers, but what about base -2? An integer n written in base -2 is a sequence of digits
n = b0 + b1(-2) + b2(-2)2 + b3(-2)3 + ...
The cool thing is that every integer (including the negative ones) has a unique base--2 representation, with no minus sign required. Your task is to find this representation.
Input
The first line of input gives the number of cases, N (at most 10000). N test cases follow. Each one is a line containing a decimal integer in the range from
Output
For each test case, output one line containing "Case #x:" followed by the same integer, written in base -2 with no leading zeros.
Sample Input Output for Sample Input
4
1
7
-2
0
Case #1: 1
Case #2: 11011
Case #3: 10
Case #4: 0
题意:将一个数转成-2进制。
思路:首先将N位可以表示的数的范围计算出来,然后依次往下看当前位是否需要为1即可。
AC代码如下:
#include<cstdio>#include<cstring>using namespace std;typedef long long ll;ll pow2[40],l[45],r[45];int main(){ int T,t,n,i,j,k,pos; ll num,f; f=1;pow2[1]=1; for(i=2;i<=35;i++) pow2[i]=pow2[i-1]*(-2); for(i=1;i<=35;i++) { l[i]=l[i-1]; r[i]=r[i-1]; if(i&1) r[i]+=f; else l[i]+=f; f*=-2; } scanf("%d",&T); for(t=1;t<=T;t++) { scanf("%lld",&num); pos=1; while(!(l[pos]<=num && num<=r[pos])) pos++; f=num; printf("Case #%d: ",t); for(i=pos;i>=1;i--) { if(l[i-1]<=num && num<=r[i-1]) printf("0"); else { printf("1"); num-=pow2[i]; } } printf("\n"); }}
- uva 11121 - Base -2
- UVA 11121 Base -2
- UVa 11121 - Base -2
- uva 11121 - Base -2
- Uva 11121 Base -2
- UVA - 11121 Base -2
- UVA 11121 Base -2
- UVa 11121 - Base -2
- UVA - 11121 Base -2
- Base -2 - UVa 11121 -2进制
- UVA 11121 - Base -2 (数论)
- UVA - 11121 Base -2 进制数
- UVa 11121 Base -2 (数论 & -2进制 & 补足思想)
- UVA 11121 - Base -2 (十进制数转化为-2进制)
- uva 11121 - Base -2(负进制转换)
- UVa 11121 Base -2 / 进制转换
- UVA - 11121 - Base -2 (负进制转换!)
- uva 11121 Base -2 (负进制计算)
- apktool 下载 教程
- 黑马程序员-------java入门笔记
- 什么是jpa
- MyEclipse,eclipse主题大全
- 图像处理中傅立叶变换的物理意义
- Base -2 - UVa 11121 -2进制
- Hduoj2544【Dijkstra】
- [leetcode]136 Single Number
- Java 枚举7常见种用法 很好的文章
- 自定义控件其实很简单2/3
- Firebug控制台详解
- android:installLocation
- 寒假集训.ID Codes
- Android 百度地图 SDK v3.0.0 (三) 添加覆盖物Marker与InfoWindow的使用