负进制转换

//输入一个10进制数,一个负进制,输出转化后的数字

#include <bits/stdc++.h> using namespace std; char a[50] = { '0','1','2','3','4', '5','6','7','8','9', 'A','B','C','D','E', 'F','G','H','I','J', 'K','L','M','N','O', 'P','Q','R','S','T', 'U','V','W','X','Y','Z' }; int main(){ int T,n,m,k,t; int base; string ans = ""; while(scanf("%d%d",&n,&base) != EOF){ printf("%d=",n); while(n != 0){ k = n % base; t = n / base; if(k < 0){ k -= base; t ++; } n = t; ans = a[k] + ans; //printf("%c",a[k]); } cout << ans; printf("(base%d) ",base); ans = ""; } return 0; }
原文地址:https://www.cnblogs.com/love-fromAtoZ/p/9077697.html