(高精度运算4.7.26)POJ 1220 NUMBER BASE CONVERSION(高精度数的任意进制的转换——方法:ba1----->10进制----->ba2)
2013-10-30 09:49
645 查看
package com.njupt.acm; import java.math.BigInteger; import java.util.Scanner; public class POJ_1220_1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while(t > 0){ BigInteger ba1 = scanner.nextBigInteger(); BigInteger ba2 = scanner.nextBigInteger(); BigInteger sum = new BigInteger("0"); String str = scanner.next(); int len = str.length(); int i; int count = 0; int temp; for(i = len - 1; i >= 0 ; --i){//先将输入的ba1进制的数按照给定的规则转化成10进制的数 char w = str.charAt(i); temp = 0; if(Character.isDigit(w)){ temp = w - '0'; }else if(Character.isLowerCase(w)){ temp = w - 'a' + 36; }else if(Character.isUpperCase(w)){ temp = w - 'A' + 10; } sum = sum.add(new BigInteger(temp + "").multiply(ba1.pow(count++))); } BigInteger zero = new BigInteger("0"); int top = 0; int stack[] = new int[2000]; while(sum.compareTo(zero) != 0){//转化成指定ba2进制的数 stack[++top] = sum.mod(ba2).intValue(); sum = sum.divide(ba2); } System.out.println(ba1+" "+str); System.out.print(ba2+" "); if(top == 0){ System.out.print(0); } while(top != 0){ char w = 0; temp = stack[top--]; if(temp < 10){ w = (char) (temp +'0'); }else if(temp>= 10 && temp < 36){ w = (char) (temp +'A' - 10); }else if(temp >= 36){ w = (char) (temp + 'a' - 36); } System.out.print(w); } System.out.println(); System.out.println(); t--; } } }
相关文章推荐
- iostream中文问题
- 关于win7设置环回接口和安装找不到指定文件的解决方法
- struts 3种从jsp页面 取值方式
- 大数据处理技术 - 基于Hadoop的实战
- Avoiding duplicate session detected errors in LCDS (and BlazeDS)
- 中国人终生受用的70个神秘偏方(一)
- dbms_space 显示自动段顾问建议!
- 五分钟学会HTML5!(一)
- org.apache.catalina.mbeans.ServerLifecycleListener
- 会计记忆总结之九:会计档案
- 上亿级数据量-JDBC插入数据库
- Unity3d截图保存到Android相册的实现
- oracle,sqlserver,mysql 命令行 开启、关闭所需要的服务
- 【转】很有用但鲜有人知的 Linux 命令
- 将默认的Netbeans中文版设置为英文界面
- linux-Gstreamer-GTK 傻瓜组装播放器
- Oracle SQL性能优化 之一
- listview获取edittext中输入的数据,显示,并且实现可删除功能
- 这就是搜索引擎:核心技术详解
- phpMyadmin连接 mysql问题总结