c++和java 两种来写,感觉java写着要顺一些,虽然代码存在很多java的错误习惯,还是贴一下,所以一定要好好学java啊orz
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;
import java.util.*;
import java.text.*;
import java.io.*;public class Main{public static int getint(char ch){if(Character.isDigit(ch)) return (ch-'0');else if(Character.isUpperCase(ch)) return (ch-'A'+10);else return (ch-'a'+36); }public static char getchar(int i){if(i>=0&&i<=9) return (char)(i+'0');else if(i>=10&&i<=35) return (char)(i-10+'A');else return (char)(i-36+'a');}public static void main(String[] args) {Scanner cin = new Scanner(new BufferedInputStream(System.in));int t=cin.nextInt();while(t--!=0){String s;String ans=new String();int base1,base2;base1=cin.nextInt();base2=cin.nextInt();s=cin.next();BigInteger result=BigInteger.valueOf(0);for(int i=0;i<s.length();i++){result=result.multiply(result.valueOf(base1)).add(result.valueOf(getint(s.charAt(i))));}while(!result.equals(BigInteger.ZERO)){ans=getchar(result.mod(result.valueOf(base2)).intValue())+ans;result=result.divide(result.valueOf(base2));}if(ans.length()==0) ans+='0';System.out.println(base1+" "+s+'\n'+base2+' '+ans);System.out.println();}}}
c++呢就是从从第i次开始,(i=1,2,3....)每一次都从首位开始枚举字符串,不断地求商取对应进制的模,求出来对应进制第i位上的数,直至结束。