题目1080:进制转换
时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:3356
解决:708
-
题目描述:
-
将M进制的数X转换为N进制的数输出。
-
输入:
-
输入的第一行包括两个整数:M和N(2<=M,N<=36)。
下面的一行输入一个数X,X是M进制的数,现在要求你将M进制的数X转换成N进制的数输出。
-
输出:
-
输出X的N进制表示的数。
-
样例输入:
-
16 10 F
-
样例输出:
-
15
-
提示:
-
输入时字母部分为大写,输出时为小写,并且有大数据。
import java.math.BigInteger; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main{ /** * @param args */ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while( scanner.hasNext() ){ String m = scanner.next(); String n = scanner.next(); String x = scanner.next(); BigInteger bigM = new BigInteger(m); BigInteger bigN = new BigInteger(n); BigInteger b = new BigInteger("1"); BigInteger a = new BigInteger("0"); for (int i = x.length()-1; i >=0 ; i--) { char c = x.charAt(i); int t; if(c >= '0' && c<= '9'){ t = c - '0'; }else{ t = c - 'A' + 10; } a = a.add(b.multiply(new BigInteger(String.valueOf(t)))); b = b.multiply(bigM); } List<Character> resultList = new ArrayList<Character>(); do{ int t = a.mod(bigN).intValue(); char c = 0; if(t >= 10) c = (char) (t - 10 + 'a'); else c = (char) (t + '0'); resultList.add(c); a = a.divide(bigN); }while( !a.equals(new BigInteger("0"))); for (int i = resultList.size()-1; i >=0 ; i--) { System.out.print(resultList.get(i)); } System.out.println(); } } } /************************************************************** Problem: 1080 User: yihukurama Language: Java Result: Accepted Time:1150 ms Memory:102916 kb ****************************************************************/

本文详细介绍了如何使用Java语言实现M进制到N进制的数转换算法,包括输入处理、转换逻辑以及输出格式说明。

被折叠的 条评论
为什么被折叠?



