ZOJ 1352 Number Base Conversion

来源:互联网 发布:js实现点击收缩和展开 编辑:程序博客网 时间:2024/05/16 15:57

才发现用java编写大数运算问题,真的是好简单啊!下面的代码是copy别人的,以后要注意java的编写了。。。

第一次用java 提交,提交时要注意:必须使用public class Main。。。

import java.util.Scanner;
import java.math.BigInteger;


public class Main {
    public static void main(String[] args) {
Scanner input = new Scanner(System.in);


int cnt = input.nextInt();
while(cnt-- != 0) {
   int a = input.nextInt();
   int b = input.nextInt();
   String x = input.next();
   String y = "";


   BigInteger A = new BigInteger(Integer.toString(a));
   BigInteger B = new BigInteger(Integer.toString(b));
   BigInteger n = new BigInteger("0");
   for(int i = 0; i < x.length(); i++) {
if(x.charAt(i) >= '0' && x.charAt(i) <= '9')
   n = n.add(new BigInteger(Integer.toString(x.charAt(i) - '0')));
else if (x.charAt(i) >= 'A' && x.charAt(i) <= 'Z')
   n = n.add(new BigInteger(Integer.toString(x.charAt(i) - 'A' + 10)));
else
   n = n.add(new BigInteger(Integer.toString(x.charAt(i) - 'a' + 36)));


if(i != x.length() - 1) n = n.multiply(A);
   }


   if(n.signum() == 0) y = "0";
   while(n.signum() != 0) {
int t = n.mod(B).intValue();


if(t < 10)
   y = t + y;
else if(t >= 10 && t < 36)
   y = (char)(t - 10 + 'A') + y;
else
   y = (char)(t - 36 + 'a') + y;


n = n.divide(B);
   }


   System.out.println(a + " " + x);
   System.out.println(b + " " + y);
   System.out.println();
}
    }
}

原创粉丝点击