#include<stdio.h> (737)#include<string.h> #define N 4000 int yuan[N],bian[N]; int conversion(int n,int from,int to){ int size=0,i,j,t; for(i=0;i<n;){ int k=0; for(j=i;j<n;j++){ t=(yuan[j]+k*from)%to; yuan[j]=(yuan[j]+k*from)/to; k=t; } bian[size++]=k; while(yuan[i]==0) i++; } return size; } //模板 char moban[62]={ '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', '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(){ char s[N];int n,i,p,q; while(scanf("%d%d",&p,&q)!=EOF){ scanf("%s",s); //1.先把字符数组转换成数值数组 for(int i=0;i<strlen(s);i++) { if(s[i]>='0'&&s[i]<='9') yuan[i]=s[i]-'0'; else if(s[i]>='A'&&s[i]<='Z') yuan[i]=s[i]-'A'+10; else yuan[i]=s[i]-'a'+36; } //2.进制转换 n=conversion(strlen(s),p,q); //3.逆序输出 输出字符因为可能有ABC for(i=n-1;i>=0;i--) { if(moban[bian[i]]>='A'&&moban[bian[i]]<='Z') moban[bian[i]]+=('a'-'A');//大写字母转换成小写字母 printf("%c",moban[bian[i]]); } printf("\n"); } return 0; }
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <vector> using namespace std; vector<char> myVector; int toInt(char c) { if (c >= '0' && c <= '9') { return c - '0'; } else { return c - 'A' + 10; } } long long convertToTen(string str, int m) { long long res = 0; for (int i = 0; i < str.size(); i++) { res *= m; res += toInt(str[i]); } return res; } char toChar(int ret) { if (ret < 10) { return ret + '0'; } else { return ret - 10 + 'a'; } } void convertToN(long long tmp, int n) { if (tmp == 0) { myVector.push_back('0'); } while (tmp) { myVector.push_back(toChar(tmp % n)); tmp /= n; } for (int i = myVector.size() - 1; i >= 0; i--) { printf("%c", myVector[i]); } cout << endl; } int main() { int m, n; long long tmp; string str; while (cin >> m >> n) { cin >> str; tmp = convertToTen(str, m); convertToN(tmp, n); myVector.clear(); } return EXIT_SUCCESS; }
#include <stdio.h> #include <string.h> char IntToChar(int x) { if(x<10) return x+'0'; else return x-10+'a'; } int CharToInt(char c){ if(c>='0'&&c<='9') return c-'0'; else if(c>='A'&&c<='Z') return c-'A'+10; else return c-'a'+10; } int main() { char s[100],ans[100]; int m,n; int len,i; long long num; while(scanf("%d %d",&m,&n)!=EOF) { scanf("%s",&s); len=strlen(s); for(num=0,i=0;i<len;i++) { num*=m; num+=CharToInt(s[i]); } i=0; while(num>0) { ans[i++]=IntToChar(num%n); num/=n; } while(i>0) printf("%c",ans[--i]); printf("\n"); } }
#include <iostream> #include <stack> #include <math.h> using namespace std; /** * 一个十进制整数转换为N进制之间的任意进制数输出 * @param ary :要转换的进制 * @param num :要转换的数 */ void Dec_To_All(int ary, long long int num) { int temp = 0; stack<char> number_stack; if (ary <= 36 || ary > 1) { //不满足条件则退出 if (ary > 1 && ary < 10) { //2-9进制 while (1) { temp = num % ary; //进制转换 number_stack.push(temp + '0'); //压栈,这里需要加上 '0' 因为栈定义为char类型 num = num / ary; //计算剩余数 if (num == 0) break; } } else if (ary == 10) //10进制 cout << num << endl; else if (ary > 10 && ary < 36) { //11-36进制 while (1) { temp = num % ary ; //进制转换 if(temp > 9) temp = temp - 10 + 'a'; //设置为a 开始,输出为a else temp = temp + '0'; //这里转换为字符 number_stack.push(temp); //压栈 num = num / ary; //计算剩余数 if (num == 0) break; } } while (!number_stack.empty()) { cout << number_stack.top(); //得到栈顶元素 number_stack.pop(); //出栈 } } } /** * 任意进制转换为10进制 * @param ary 目前的进制数 * @param num 要转换的数 * @return */ long long int All_to_Dec(int ary, string num) { long long int result = 0; long long int temp = 0, i = 0; if (ary > 36 || ary < 0) return 0; for (int j = 0; j < num.size(); ++j) { if (num[j] >= '0' && num[j] <= '9') { temp = (num[j] - '0') * pow(ary, num.size() - 1 - j); //计算这个字母对应的数字 } else if (num[j] >= 'A' && num[j] <= 'Z') { temp = (num[j] - 'A' + 10) * pow(ary, num.size() - 1 - j); //计算这个字母对应的数字 } result += temp; } return result; } /** * 将M进制的数X转换为N进制的数输出。(2<=M,N<=36) */ int main() { int M = 0, N = 0; string X = ""; //要转换的数 cin >> M >> N; //获取初始进制数M和目标进制数N cin >> X; //需要转换的数 long long int dec_num = All_to_Dec(M, X); //M进制转换为十进制数,这里需要用long long才能存下 Dec_To_All(N, dec_num); //十进制转换为N进制 return 0; }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()){ int m = scanner.nextInt(); int n = scanner.nextInt(); String num = scanner.next(); long i = Long.parseLong(num, m); System.out.println(Long.toString(i,n)); } } }
先将M进制转换为10进制数dec,在将dec转换为N进制。在提交的时候发现int类型的dec有溢出的情况,遂改为long型。
#include #include int main(){ int M,N,len,i,mod; long dec; char strNum[50],c; while(scanf("%d%d",&M,&N) != EOF){ scanf("%s",strNum); len = strlen(strNum); dec = 0; for(i=0; i<len; i++){ if(strNum[i]>='0' && strNum[i]<='9') dec = dec*M + strNum[i] - '0'; else dec = dec*M + strNum[i] - 'A' + 10; } //printf("%d\n",dec); i=0; while(dec){ mod = dec % N; dec /= N; if(mod>=0 && mod <=9) c = '0' + mod; else c = 'a' + mod - 10; strNum[i] = c; i++; } while(i){ printf("%c",strNum[i-1]); i--; } printf("\n"); } }
#include<bits/stdc++.h>
void convert(int x,int y,char a[4001],char b[4001]){//任意x进制大整数a[i]转换为y进制大整数b[j]
int j;
for(j=0;strlen(a)!=0;j++){
int yu=0,mark=0,temp,offset=0;//yu为临时余数,mark=0说明忽略除数中为零的最高位,offset为除数数组下标
for(int i=0;i<strlen(a);i++){
if(a[i]>='0'&&a[i]<='9')
temp=a[i]-'0'+yu*x;//判断是否十进制以内数
else if(a[i]>='A'&& a[i]<='Z')
temp=a[i]-'A'+10+yu*x;//判断是否超过十进制
if(temp/y!=0) mark=1;
if(mark==1)
if(temp/y<10) a[offset++]=temp/y+'0';
else a[offset++]=temp/y-10+'A';//恢复对应的字符值
yu=temp%y;
}
if(yu<10) b[j]=yu+'0';
else b[j]=yu-10+'A';//确保十进制以上的数正确存储
a[offset]='\0';
}
b[j]='\0';
}//转换结果倒序存放
int main(){
char a[4001],b[4001];
int m,n;
while(scanf("%d %d ",&m,&n)!=EOF){
gets(a);
convert(m,n,a,b);
for(int i=strlen(b)-1;i>=0;i--)
if(b[i]>='A') printf("%c",b[i]-'A'+'a');
else printf("%c",b[i]);//结果倒序输出
printf("\n");
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
}
}
#include<stdio.h>
#include<string.h>
#define maxSize 10
struct bigint{
int num[maxSize];
int size;
void init(){
for(int i=0;i<maxSize;i++) num[i]=0;
size=0;
}
void set(int x){//普通整数----->>高精度整数
init();
do{
num[size++]=x%10000;
x=x/10000;
}while(x!=0);
}
void output(){
for(int i=size-1;i>=0;i--){
if(i!=size-1) printf("%04d",num[i]);
else printf("%d",num[i]);
}
printf("\n");
}
bigint operator * (int x) const{//高精度数与普通整数乘积
bigint ret;
ret.init();
int c=0;
for(int i=0;i<size;i++){
int t=num[i]*x+c;
ret.num[ret.size++]=t%10000;
c=t/10000;
}
if(c!=0){
ret.num[ret.size++]=c;
}
return ret;
}
bigint operator + (const bigint &a) const{//高精度整数与高精度整数加法
bigint ret;
ret.init();
int c=0;
for(int i=0;i<size||i<a.size;i++){
int t=num[i]+a.num[i]+c;
ret.num[ret.size++]=t%10000;
c=t/10000;
}
if(c!=0){
ret.num[ret.size++]=c;
}
return ret;
}
bigint operator / (int x) const{//高精度整数对普通整数求商
bigint ret;
ret.init();
int r=0;
for(int i=size-1;i>=0;i--){
int t=num[i]+r*10000;
ret.num[i]=t/x;
r=t%x;
}
ret.size=0;
for(int i=0;i<maxSize;i++){
if(ret.num[i]!=0)
ret.size=i;
}
ret.size++;
return ret;
}
int operator % (int x) const{//高精度整数对普通整数求余数
int r=0;
for(int i=size-1;i>=0;i--){
r=(num[i]+r*10000)%x;
}
return r;
}
}sum,mi;//十进制和 累加中间值 幂
int main(){
int m,n;
char x[1000];
char ans[1000];
while(scanf("%d%d",&m,&n)!=EOF){
scanf("%s",x);
int L=strlen(x);
sum.set(0);//累加得到的十进制值
mi.set(1);
for(int i=L-1;i>=0;i--){
int t;
if(x[i]>='0'&&x[i]<='9') t=x[i]-'0';
else t=x[i]-'A'+10;
sum=sum+mi*t;
mi=mi*m;
}
int size=0;
do{
int t=sum%n;
if(t>=10) ans[size++]=t-10+'a';
else ans[size++]=t+'0';
sum=sum/n;
}while(!(sum.num[0]==0&&sum.size==1));
for(int i=size-1;i>=0;i--){
printf("%c",ans[i]);
}
printf("\n");
}
return 0;
}
#include <iostream> #include <string> #include "math.h" using namespace std; //m进制的数s,转换成n进制 void fn(int m,int n,string s){ //转换10进制 long long sum=0; int l=s.size(); for(int i=0;i<l;i++){ int num; if(s[i]>='0'&&s[i]<='9') num=s[i]-'0'; else num=s[i]-'A'+10; sum+=num*pow(m,l-i-1); } //cout<<"sum:"<<sum<<",转换为"<<n<<"进制"<<endl; //转换n进制 char x[1000];int ln=0; while(sum>0){ int t=sum%n; if(t<=9) x[ln]=t+'0'; else x[ln]=t-10+'a'; ln++; sum/=n; } for(int i=ln-1;i>=0;i--) cout<<x[i]; cout<<endl; } int main(){ int m,n; string s; while(cin>>m>>n>>s){ fn(m,n,s); } }看起来没有涉及到大数的问题,把10进制的数用long long 存储没有问题
#include <algorithm> #include <string> #include <iostream> using namespace std; /*M进制数转N进制,2<=M,N<=36。 输入时如有字母,则字母为大写,输出时如有字母,则字母为小写*/ int char_to_num(char c)//将字符变成数字 { int a; if (c >= 'A' && c <= 'Z') a = c - 'A' + 10; else a = c - '0'; return a; } char num_to_char(int n)//将数字变成字符 { char c; if (n >= 10) c = n - 10 + 'A';//结果是字符 else c = n + '0';//结果是数字 return c; } string conversion(int pre, string num, int cur) //pre进制数字num转换为cur进制 { int i = 0; char rem;//每一步的余数,用字符型是为了方便result插入类型统一 string result; while (i < num.size()) { rem = '0'; for (int j = i; j < num.size(); j++) { int a, b;//a,b代表数字 a = char_to_num(rem); b = char_to_num(num[j]); int temp = (a * pre + b);//用数字计算每步结果 //还原为字符 num[j] = num_to_char(temp / cur); rem = num_to_char(temp % cur); } result.insert(0, 1, rem);//头插结果 while (num[i] == '0') i++;//跳过高位的0 } for (int k = 0; k < result.size(); k++)//输出字母小写 if (result[k] >= 'A' && result[k] <= 'Z') result[k] += ('a' - 'A'); return result; } int main() { int M, N; string num; while (cin >> M >> N) { cin >> num; cout << conversion(M, num, N) << endl; } }所有进制转换都是统一模式。含字符改进
#include<cstdio> (802)#include<iostream> #include<vector> (721)#include<string> #include<cctype> using namespace std; int n,m; //注意输入时如有字母,则字母为大写,输出时如有字母,则字母为小写。 long long ToNum(string x,int m){ long long ret=0; for(int i=0;i<x.size();i++){ int t; if(isdigit(x[i])){ t=x[i]-'0'; }else { t=x[i]-'A'+10; } ret=ret*m+t; } return ret; } string ToString(long long x,int n){ string ret=""; while(x!=0){ int t=x%n; if(t>=0&&t<=9){ char c= t+'0'; ret=c+ret; }else{ char c=t-10+'a'; ret=c+ret; } x/=n; } return ret; } int main(){ while(cin>>m>>n){ string x; cin>>x; long long tmp=ToNum(x,m); string ans=ToString(tmp,n); cout<<ans<<endl; } return 0; }
#include #include using namespace std; int main() { int m,n; while(cin>>m>>n){ char x[1000]; long long num=0; scanf("%s",x); for(int i=0;x[i]!='\0';i++){ num*=m; if('0'<=x[i]&&x[i]<='9'){ num+=x[i]-'0'; } else{ num+=x[i]-'A'+10; } } if(num==0) cout<<0; else{ char b[1000]; int j=0; while(num>0){ if(num%n<10){ b[j++]=num%n+'0'; } else{ b[j++]=num%n-10+'a'; } num/=n; } for(int k=j-1;k>=0;k--){ cout<<b[k]; } } cout<<endl; } return 0; } // 64 位输出请用 printf("%lld")
#include <iostream> #include <string> using namespace std; int toNum(char c){ int n=0; if(c>='0'&&c<='9') return c-'0'; else return c-'A'+10; } char toChar(int n){ if(n<10) return n+'0'; else return n-10+'A'; } int main(){ string str; int m,n,remind,t; cin>>m>>n; string s,result; long long temp; while(cin>>s){ temp=0; t=0; for(int i=0;i<s.size();i++){ temp*=m; t=toNum(s[i])+t; temp+=t%m; t/=m; } //cout<<temp<<endl; result=""; while(temp!=0){ remind=temp%n; result=toChar(remind)+result; temp/=n; } cout<<result<<endl; } return 0; } //先转十进制,再转其他进制
import java.math.BigInteger; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(scanner.hasNext()) { //将m进制的数转化成n进制的数 int m = scanner.nextInt(); int n = scanner.nextInt(); String x = scanner.next(); Map<Character, Integer> myMap = new HashMap<Character, Integer>(); myMap.put('0',0);myMap.put('1',1 );myMap.put('2',2 );myMap.put('3',3 );myMap.put('4', 4); myMap.put('5',5 );myMap.put('6', 6);myMap.put('7',7 );myMap.put('8',8 );myMap.put('9',9 ); myMap.put('A',10 );myMap.put('B',11 );myMap.put('C', 12);myMap.put('D',13 );myMap.put('E',14 ); myMap.put('F',15 );myMap.put('G',16 );myMap.put('H',17 );myMap.put('I',18 );myMap.put('J',19 ); myMap.put('K',20 );myMap.put('L',21 );myMap.put('M',22 );myMap.put('N',23 );myMap.put('O',24 ); myMap.put('P',25 );myMap.put('Q',26 );myMap.put('R',27 );myMap.put('S',28 );myMap.put('T',29 ); myMap.put('U',30 );myMap.put('V',31 );myMap.put('W',32 );myMap.put('X',33 );myMap.put('Y',34 ); myMap.put('Z', 35); BigInteger bi = BigInteger.ZERO; for (int i = x.length()-1; i >= 0; i--) { bi = bi.add(BigInteger.valueOf((long)(myMap.get(x.charAt(i)) * Math.pow(m, x.length()-i-1) )) ); } System.out.println(bi.toString(n)); } } }
import sys r2 = {} r3 = {} for i in range(0, 26): r2[chr(ord('a') + i)] = i + 10 r2[chr(ord('A') + i)] = i + 10 r3[i + 10] = chr(ord('a') + i) r3[i + 10] = chr(ord('A') + i) for i in range(10): r2[str(i)] = i r3[i] = str(i) m, n = list(map(int, input().split())) aim = input() aim_nums = 0 index = 0 for a in aim[::-1]: aim_nums += r2[a] * (m ** index) index += 1 results = "" while aim_nums > 0: results += r3[aim_nums % n] aim_nums //= n print(results[::-1])