http://acm.hust.edu.cn/vjudge/contest/129733#problem/Fios
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.
git
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 .
express
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.
app
to-2 10 from-2 1010 to-10 10 to-10 -10 from-10 10 end
11110 -10 190 10 -10
2016-HUST-線下組隊賽-2
url
對給定的數,從指定負進制轉成十進制,或從十進制轉成負進制數.
spa
from操做跟正進制同樣,比較簡單.
對於to操做:負進制數
數n轉成正進制r的過程:不停地模r、除r,直到n爲0. 獲得的餘數序列就是結果.
當r是負數時,進行上述過程可能會獲得負餘數,這不符合結果.
因此在獲得負餘數時,商加一,餘數減radix,使得餘數爲正再進行下一步.
比賽時用的暴力枚舉,根據當前位表示數的範圍和惟一性來肯定每一位數. 遠不如這個簡潔.
code
#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=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; }