#include<bits/stdc++.h> using namespace std; /*函数:r进制转换成10进制*/ int rToTen(string n,int r){ //将r进制转为10进制,n是该r进制的字符串表示 int len = n.length(); int ans = 0; int i = 0; while(i<len){ ans*=r; ans+=n[i]-'0'; i++; } return ans; } /*函数:十进制转任意进制*/ string tenToR(int n,int r){ //十进制n转r进制 ,返回字符串s char str[1005]; stack<int>s; while(n){ s.push(n%r); n = n/r; } int i = 0; while(!s.empty()){ switch(s.top()){ case 10: str[i++] = 'A'; break; case 11: str[i++] = 'B'; break; case 12: str[i++] = 'C'; break; case 13: str[i++] = 'D'; break; case 14: str[i++] = 'E'; break; case 15: str[i++] = 'F'; break; default: str[i++] = s.top()+'0'; } s.pop(); } string st = str; return st; } int main(){ cout<<rToTen("10010",2)<<endl; cout<<tenToR(18,2)<<endl; }