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:
18 4 +78 3 +08 2 +28 1 +48 0 = 14096 + 7512 + 28 + 41 = 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 = 14096 - 7512 - 28 + 41 = 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操作:负进制数
数n转成正进制r的过程:不停地模r、除r,直到n为0. 得到的余数序列就是结果.
当r是负数时,进行上述过程可能会得到负余数,这不符合结果.
所以在得到负余数时,商加一,余数减radix,使得余数为正再进行下一步.
比赛时用的暴力枚举,根据当前位表示数的范围和唯一性来确定每一位数. 远不如这个简洁.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <list>
#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=0int 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;
}

转载于:https://www.cnblogs.com/Sunshine-tcf/p/5799724.html

UVALive 3958 Weird Numbers (负进制数)相关推荐

  1. 十六进制190的2进制数_十六进制数系统解释

    十六进制190的2进制数 Hexadecimal numbers, often shortened to "hex numbers" or "hex", are ...

  2. 【进制转换】负进制转换 多进制转换

    1.负进制转换 洛谷P1017 进制转换 负进制的转换和正进制的转换雷同,都是用短除取余法,这里要保证余数都要是正数才行: 要想让本来余数是负数变为正数,其实很好操作,就直接让商+1,余数-(相应的进 ...

  3. 超详细介绍!!!带你认识各种进制数的及其转换以及原码,反码,补码

    1.本文详细介绍了二进制,八进制,八进制和他们之间的相互转换 2.也详细介绍了原码,反码,补码以及它们之间的相互转换,让你更加了解计算机数据的底层运行逻辑 目录 一:认识各种进制数 1.十进制数 2. ...

  4. 利用栈实现把十进制数转换为二进制至十六进制之间的任一进制数并输出的功能。(第二版)

    [实验题目内容] 保持计算机默认的十进制不变(要求不用C++流操纵符转换基数为八进制形式oct,不用C++流操纵符转换基数为十六进制形式hex,也不用setbase(base)函数将基数设置为base ...

  5. 2021微软暑期实习机试,负进制转换,十进制转-2进制

    2021微软暑期机试题,进制转换 一 前言 二 解题步骤 2.1 题目描述 2.1.1 简述前两题 2.1.2 2.2解题思路 2.3代码思路 三 问题和启发 3.1 遇到的一些问题 3.2 考虑-3 ...

  6. P1066 2^k进制数 NOIP 2006 提高组 第四题

    洛谷蓝题(点击跳转) 提高组 第四题 题目描述 设r是个2^k 进制数,并满足以下条件: (1)r至少是个2位的2^k 进制数. (2)作为2^k 进制数,除最后一位外,r的每一位严格小于它右边相邻的 ...

  7. (C++)除基取余法:将十进制数转化为Q进制数

    所谓基,就是指将要转换成的进制Q. 除基取余的意思就是:每次将待转换数除以Q,然后将得到的余数作为低位存储,而商则继续除以Q并重复上面的操作,直至商0时,将所有位从高到低输出就可以得到Q进制数. 代码 ...

  8. 16进制数组转成10进制 qt_计算机组成原理(进制数及转换)

    图片来源于网络 都知道计算机数据是以二进制数0和1补码的形式存储在内存中. 那你知道它们转换关系吗?那么问题来了,为什么要转换? 前面已经说过计算机数据是以二进制0和1存储,所以它们要转换为二进制存储 ...

  9. [NOIP2006] 提高组 洛谷P1066 2^k进制数

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

最新文章

  1. Equals方法与==运算符的使用
  2. 配置bond和vlan
  3. 解析法实现一元线性回归、多元线性回归以及数据模型可视化操作
  4. C++ 多态和虚函数
  5. Magento: 判断是否为手机浏览 Optimise Web's Mobile Detect Class for Magento
  6. android侧边栏点击,侧边菜单栏 android-menudrawer
  7. 基于单片机的“彩灯控制器”的程序设计与调试
  8. 一台电脑同时启动多个java_一台电脑如果运行多个相同的程序
  9. 开放平台-web实现人人网第三方登录
  10. 润乾报表设计器——预览报表问题解决
  11. 鲁豫有约怎么下载,怎么实现qlv转MP4
  12. 支付宝(蚂蚁金服开放平台)-支付结果异步通知-验签
  13. 阿里P6和P7待遇差别有多大网友干的活差不多,工资差很多
  14. kit_00_001-为创建新的虚拟机做准备
  15. Ubuntu 16.04 安装Tensorflow Object Detection API (一)
  16. 管理学定律十:罗森塔尔效应与虚假同感偏差
  17. STM32精英版(正点原子STM32F103ZET6开发板)学习篇13——ssd1306OLED实验
  18. mysql rollback如何使用_MYSQL的COMMIT和ROLLBACK使用讲解
  19. 算法描述 100的阶乘
  20. 9.脚本语言知识总结

热门文章

  1. Spring 7大功能模块的作用[转]
  2. nginx 在ubuntu 上的启动,停止,重启
  3. C++primer plus第六版课后编程题答案8.6
  4. maven配置篇之pom.xml
  5. mysql判断存在返回布尔_MySqlClient访问tinyint字段返回布尔值篇
  6. 城市轨道交通运营票务管理论文_城市轨道交通运营企业的票务组织管理
  7. python 数学公式显示_ipython jupyter notebook中显示图像和数学公式实例
  8. 连接impala出现method not supported_Impala在网易大数据的优化和实践
  9. HDFS节点内数据平衡
  10. 关于我的FPGA博客