负进制
来源:互联网 发布:西安建筑科技大学网络 编辑:程序博客网 时间:2024/06/05 07:45
C - Weird Numbers
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uDescription
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 R^P . This means the value of the digit is multiplied by R^P 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.
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 R^P . This means the value of the digit is multiplied by R^P 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 .
A conversion to the decimal system will start with a lowercase word “from”, followed by a minus sign, radix R, one space, and a number written in the system with a base of -R.
The input will be terminated by a line containing a lowercase word “end”. All numbers will satisfy the following conditions: 2 ≤ R ≤ 10, -1000000≤N≤1000000 (decimal).
A conversion to the decimal system will start with a lowercase word “from”, followed by a minus sign, radix R, one space, and a number written in the system with a base of -R.
The input will be terminated by a line containing a lowercase word “end”. All numbers will satisfy the following conditions: 2 ≤ R ≤ 10, -1000000≤N≤1000000 (decimal).
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.
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 10from-2 1010to-10 10to-10 -10from-10 10end
Sample Output
11110-1019010-10
#include<stdio.h>#include<math.h>#include<string.h>#include<set>using namespace std;int a[200]={0},p;//负进制转换,数除要转的进制数,如果余数小于0,余数加上所转进制的绝度值,然后商+1,余数反转即为负进制 char b[3000];void ff(int x,int y){int t,t1;p=0;while(y!=0){t=y%x;y=y/x;if(t<0){t+=abs(x);y++;}a[p++]=t;}}int f(int x,char b[]){int t,sum=0,i=0,j; for(i=strlen(b)-1,j=0;i>=0;i--,j++) { sum+=(b[i]-'0')*pow(x,j); }return sum;}int main( ){ char c; int r,i,t; while(~scanf("%c",&c)&&c!='e') { if(c=='t') { getchar(); scanf("%d %d",&r,&t); ff(r,t); if(p==0) p++; for(i=p-1;i>=0;i--) { printf("%d",a[i]);} } if(c=='f') { getchar();getchar();getchar(); scanf("%d %s",&r,b); printf("%d",f(r,b)); } getchar(); printf("\n");} }
阅读全文
0 0
- 负进制
- 负进制转换
- 负进制转换
- 负进制的转换
- POJ-3191(负进制)
- 10.6 负进制 2047
- NEUQ 1105 负进制的转换
- uva 11121 - Base -2(负进制转换)
- UVA - 11121 - Base -2 (负进制转换!)
- uva 11121 Base -2 (负进制计算)
- POJ-3191(负进制那点事儿)
- noip2000进制转换 (负进制应用)
- 进制转化(尤其是负进制)
- UVa 11121 - Base -2 负进制的转化和推广
- 2017年10月05日普及组 负进制
- C问题---任意进制转换问题(包括负进制)
- The Moronic Cowmpouter poj3191(负进制转换以及其他进制转换模板)
- Android框架 MVC,MVP和MVVM区别对比
- Appium 新手贴:Windows 平台上的使用 Python 语言实现 appium 自动化程序 for Android (完整版)
- Echarts动态加载地图数据(Dynamic load Echarts map data)
- jvm方法区的回收
- OC基础-野指针和僵尸对象22
- 负进制
- Apache服务器安装https证书
- 初识Nginx--Linux下的基础命令
- Struts2 输入验证与自定义拦截器
- 南京邮电大学2013年硕士研究生招生复试及同等学力加试参考书目
- Qt提升对SQLite数据库的读写速度
- 高通LCD kernel层代码跟踪分析
- Struts2系列之文件上传下载
- 循环json数据