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;

}

最新文章

  1. WIN8 平台应用隐私声明
  2. Android之捕获TextView超链接
  3. IOS-TextField控件详解
  4. dict内部方法
  5. Leetcode#123 Best Time to Buy and Sell Stock III
  6. Lucene学习总结之三:Lucene的索引文件格式(1)
  7. SpringMVC+Spring3+Hibernate4开发环境的搭建
  8. iOS之Xcode修改应用图标
  9. OSI网络模型
  10. 爬虫值requests库
  11. Android 7.0 启动篇 — init原理(二)(转 Android 9.0 分析)
  12. 【Mac】【问题】
  13. 基于python调用libvirt API
  14. Ubuntu14.04 编译 Android 5.1.1源码(采用国内镜像)
  15. 2015-09-22 css2
  16. redis.conf配置详解(转)
  17. 【laravel5.*】详解laravel中的依赖注入
  18. 【WPF】拖拽ListBox中的Item
  19. css文字闪烁效果
  20. 70.如何在xilinx SDK中显示行号

热门文章

  1. P1311选择客栈
  2. [ERROR] Plugin org.apache.maven.plugins:maven-clean-plugin:2.5 or one of its dependencies could not be resolved: Cannot access nexus-aliyun (http://maven.aliyun.com/nexus/content/groups/public) in off
  3. 并发编程时守护进程在pycharm与python shell中的运行结果不同
  4. [51Nod1850] 抽卡大赛
  5. sublime3故障收集emmet无法安装pyv8
  6. LINUX 必知必会检测表--通读无关语言
  7. ELK7.X中配置x-pack
  8. 使用vim打造python-ide
  9. 搭建jumperserver堡垒机管理万台服务器-2
  10. 判断用户输入YES或NO