Weird Numbers

题目链接:

http://acm.hust.edu.cn/vjudge/contest/129733#problem/F

Description


Binary numbers form the principal basis of computer science. Most of you have heard of other systems, such as ternary, octal, or hexadecimal. You probably know how to use these systems and how to convert numbers between them. But did you know that the system base (radix) could also be negative? One assistant professor at the Czech Technical University has recently met negabinary numbers and other systems with a negative base. Will you help him to convert numbers to and from these systems?
A number N written in the system with a positive base R will always appear as a string of digits between 0 and R - 1 , inclusive. A digit at the position P (positions are counted from right to left and starting with zero) represents a value of RP . This means the value of the digit is multiplied by RP and values of all positions are summed together. For example, if we use the octal system (radix R = 8 ), a number written as 17024 has the following value:
1*8 4 +7*8 3 +0*8 2 +2*8 1 +4*8 0 = 1*4096 + 7*512 + 2*8 + 4*1 = 7700
With a negative radix - R , the principle remains the same: each digit will have a value of (- R)P . For example, a negaoctal (radix R = - 8 ) number 17024 counts as:
1*(- 8) 4 +7*(- 8) 3 +0*(- 8) 2 +2*(- 8) 1 +4*(- 8) 0 = 1*4096 - 7*512 - 2*8 + 4*1 = 500
One big advantage of systems with a negative base is that we do not need a minus sign to express negative numbers. A couple of examples for the negabinary system (R = - 2) :
You may notice that the negabinary representation of any integer number is unique, if no "leading zeros" are allowed. The only number that can start with the digit "0", is the zero itself.

Input


The input will contain several conversions, each of them specified on one line. A conversion from the decimal system to some negative-base system will start with a lowercase word "to" followed by a minus sign (with no space before it), the requested base (radix) R , one space, and a decimal number N .

Output


For each conversion, print one number on a separate line. If the input used a decimal format, output the same number written in the system with a base - R . If the input contained such a number, output its decimal value.
Both input and output numbers must not contain any leading zeros. The minus sign "-" may only be present with negative numbers written in the decimal system. Any non-negative number or a number written in a negative-base system must not start with it.

Sample Input


```
to-2 10
from-2 1010
to-10 10
to-10 -10
from-10 10
end
```

Sample Output


```
11110
-10
190
10
-10
```

Source


2016-HUST-线下组队赛-2


##题意:

对给定的数,从指定负进制转成十进制,或从十进制转成负进制数.


##题解:

from操作跟正进制一样,比较简单.
对于to操作:[负进制数](http://baike.baidu.com/link?url=hk4TMW9mCVKEAWfDVCECQAFJ_GXcQJTMgh1juL2R4lCjxL6vzVi4ufEHF9cn-aWSReWaorH0kFLheWTWhx1JzK)
数n转成正进制r的过程:不停地模r、除r,直到n为0. 得到的余数序列就是结果.
当r是负数时,进行上述过程可能会得到负余数,这不符合结果.
所以在得到负余数时,商加一,余数减radix,使得余数为正再进行下一步.
比赛时用的暴力枚举,根据当前位表示数的范围和唯一性来确定每一位数. 远不如这个简洁.


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 25
#define mod 100000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;

int radix;

char str[50];

int main(int argc, char const *argv[])

{

//IN;

while(scanf("%s", str) != EOF && str[0] != 'e')
{
if(str[0] == 'f') {
sscanf(str+4, "%d", &radix);
char num[50];
scanf("%s", num); int ans = 0;
for(int i=0; i<strlen(num); i++) {
ans = ans*radix + num[i] - '0';
}
printf("%d\n", ans);
} else {
sscanf(str+2, "%d", &radix);
int n; scanf("%d", &n);
int ans[50] = {0}, cnt = 0; do { // 处理 n=0
int last = n % radix;
n = n / radix;
if(last < 0) last -= radix, n += 1;
ans[cnt++] = last;
} while(n); for(int i=cnt-1; i>=0; i--)
printf("%d", ans[i]);
printf("\n");
}
} return 0;

}

UVALive 3958 Weird Numbers (负进制数)的更多相关文章

  1. CF459C Pashmak and Buses (构造d位k进制数

    C - Pashmak and Buses Codeforces Round #261 (Div. 2) C. Pashmak and Buses time limit per test 1 seco ...

  2. C++中,将单精度浮点数转换成2进制数

    在C++里,实数(float)是用四个字节即三十二位二进制位来存储的.其中有1位符号位,8位指数位和23位有效数字位.实际上有效数字位是24位,因为第一位有效数字总是"1",不必存 ...

  3. [转]as3 算法实例【输出1 到最大的N 位数 题目:输入数字n,按顺序输出从1 最大的n 位10 进制数。比如输入3,则输出1、2、3 一直到最大的3 位数即999。】

    思路:如果我们在数字前面补0的话,就会发现n位所有10进制数其实就是n个从0到9的全排列.也就是说,我们把数字的每一位都从0到9排列一遍,就得到了所有的10进制数. /** *ch 存放数字 *n n ...

  4. 1813. M进制数问题

    1813. M进制数问题 Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description 试用 C++的类来表示一般进制数. 给定 2 ...

  5. [codevs1157]2^k进制数

    [codevs1157]2k进制数 试题描述 设r是个2k 进制数,并满足以下条件: (1)r至少是个2位的2k 进制数. (2)作为2k 进制数,除最后一位外,r的每一位严格小于它右边相邻的那一位. ...

  6. noip2006 2^k进制数

    设r是个2k进制数,并满足以下条件: (1)r至少是个2位的2k进制数. (2)作为2k进制数,除最后一位外,r的每一位严格小于它右边相邻的那一位. (3)将r转换为2进制数q后,则q的总位数不超过w ...

  7. c++描述将一个2进制数转化成10进制数(用到初始化栈,进栈,入栈)

    /* c++描述将2进制数转化成10进制数 问题,1.初始化栈后,用new,不知道delete是否要再写一个函数释放内存, 还是在哪里可以加上delete 2.如果栈满了,我要分配多点空间,我想的办法 ...

  8. 关于不同进制数之间转换的数学推导【Written By KillerLegend】

    关于不同进制数之间转换的数学推导 涉及范围:正整数范围内二进制(Binary),八进制(Octonary),十进制(Decimal),十六进制(hexadecimal)之间的转换 数的进制有多种,比如 ...

  9. NOIP2006 2k进制数

    2^k进制数 题目描述 设r是个2^k 进制数,并满足以下条件: (1)r至少是个2位的2^k 进制数. (2)作为2^k 进制数,除最后一位外,r的每一位严格小于它右边相邻的那一位. (3)将r转换 ...

随机推荐

  1. 七、Zabbix-模板,应用集,监控项,触发器

    本篇内容,将模板,应用集,监控项,触发器放在一起,因为我们建立使用的监控项和触发器,大多数都是对多台机器使用的,很少有一个监控项对应一个主机的情况. 一.模板 1.什么是模板? 个人理解,模板就是模板 ...

  2. k线图的分形

    蜡烛图上的分形指标,作为一种特殊的K线组合形态,通过对价格的一系列的高低点的描述,辅助识别出市场潜在的突破和反转点,预判后期走势. 顶分形:相邻的五根K线,若中间那根K线最高价为这五根K线的最高价,则 ...

  3. [转帖]虚拟内存探究 -- 第一篇:C strings & /proc

    虚拟内存探究 -- 第一篇:C strings & /proc http://blog.coderhuo.tech/2017/10/12/Virtual_Memory_C_strings_pr ...

  4. [AGC040B]Two Contests

    Description 给出若干条线段 \((L[i], R[i])\) ,把他们分成两个非空的集合,最大化集合内线段交的和. \(n\le 10 ^ 5\) Solution 考虑最小的一个右端点 ...

  5. vue router应用及总结

    编写一个小的demo,对router基础的应用学习和理解. 效果图示: 说明: 点击About在右边显示相关信息. 说明: 点击Home,在下边显示相关信息,且Home下有两个路由链接,分别对应各自的 ...

  6. [BZOJ5306] [HAOI2018]染色(容斥原理+NTT)

    [BZOJ5306] [HAOI2018]染色(容斥原理+NTT) 题面 一个长度为 n的序列, 每个位置都可以被染成 m种颜色中的某一种. 如果n个位置中恰好出现了 S次的颜色有 K种, 则小 C ...

  7. VLAN原理详解[转载] 网桥--交换机---路由器

    来自:http://blog.csdn.net/phunxm/article/details/9498829 一.什么是桥接          桥接工作在OSI网络参考模型的第二层数据链路层,是一种以 ...

  8. 解决Ubuntu环境下在pycharm中导入tensorflow报错问题

    环境: Ubuntu 16.04LTS anacoda3-5.2.0 问题: ImportError: No module named tensorflow 原因:之前安装的tensorflow所用到 ...

  9. mailto - 简单多媒体邮件发送程序

    SYNOPSIS mailto  [-a] [-c] [-s] [recipient name(s)] DESCRIPTION mailto 程序是一个用于发送MIME格式的多媒体邮件(MIME格式是 ...

  10. [转]WAREZ无形帝国

    一. 这会儿夜深了,他们昏昏睡去.随便哪栋建筑的某一个黑洞洞的窗口,你冷眼望去,没准就能看到一台白色的电脑,静静地卧在主人的书桌上.如果那主人睡得足够深,你就打开他的抽屉,现在你看到了什么?哦,我不是 ...