UVa 11121 - Base -2
来源:互联网 发布:僵直脊柱炎周杰伦知乎 编辑:程序博客网 时间:2024/06/06 23:11
题目:计算以-2为基数的数的表示。
分析:数论。写出不同位数能表示的数字区间就可以找到规律。
长度为1:[1,1]; 长度为2:[-2,-1]; 长度为3:[2,5];
观察发现,区间长度增长为1,2,4,8,..,2^k,并且奇偶间隔开;
这样可以按顺序找到对应的1的位置,每次减去对应的基底(-2^k)寻找下一个1的位置即可。
说明:又是数论。
#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>using namespace std;long long l[40],r[40],b[40];int ans[40];int main(){l[0] = 1; r[0] = 1; b[0] = 1;l[1] = -2;r[1] = -1;b[1] = -2;long long base = 4;for (int i = 2 ; i < 33 ; ++ i) {if (i%2) {r[i] = l[i-2]-1;l[i] = l[i-2]-base;}else {l[i] = r[i-2]+1;r[i] = r[i-2]+base;}base <<= 1;b[i] = b[i-1]*-2;}int n,m,s,e;while (~scanf("%d",&n))for (int i = 1 ; i <= n ; ++ i) {scanf("%d",&m);printf("Case #%d: ",i);for (int i = 0 ; i < 33 ; ++ i)ans[i] = 0;s = 32;while (s >= 0) {if (m >= l[s] && m <= r[s]) {ans[s] = 1;m -= b[s];}s --;}e = 32;while (e > 0 && !ans[e]) e --;while (e >= 0) printf("%d",ans[e --]);printf("\n");}return 0;}
0 0
- 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
- 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 (负进制转换!)
- uva 11121 Base -2 (负进制计算)
- UVA 11121 Base -2 (进制转换)
- UVa 11121 Base -2 (数论 & -2进制 & 补足思想)
- omap4-gpio
- CodeVs 1506 传话
- equals 和 ==
- Android开发系列(二十四):Notification的功能与用法
- java 将jso 格式的字符串转换为List<Map<String,String>>
- UVa 11121 - Base -2
- hdu 1075 What Are You Talking About(字典树)
- 20款让人惊叹的html5动画效果
- Minimum Depth of Binary Tree
- C语言编程代码分析0
- C/C++中static关键字详解
- The Triangle
- zoj 3229 (有上下届的最大流)
- 保龄球-----计分程序