ZOJ 1352 Number Base Conversion

11 篇文章 0 订阅
4 篇文章 0 订阅

才发现用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();
}
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值