Why so serious? --[NKU]schindlerlee

pku1220

schindlerlee原创,禁止转载和用于商业用途

此题意思很明显
把一个数从进制a转化到进制b
用C/C++写实在是太麻烦了,用java搞了一下,还是不熟啊,写了好久
 1 //java catalan 数
 2 import java.math.*;
 3 import java.util.*;
 4 import java.io.*;
 5 
 6 public class Main {
 7 
 8     public static void main(String[] args) {
 9         Scanner cin = new Scanner(
10                 new BufferedReader(
11                 new InputStreamReader(System.in)));
12         //That is (in decimal) A = 10, B = 11, , Z = 35, a = 36, b = 37, , z = 61
13         int c = cin.nextInt();
14         while (c-- > 0) {
15             int a = cin.nextInt();
16             int b = cin.nextInt();
17             BigInteger r = BigInteger.valueOf(0);
18             BigInteger idx = BigInteger.valueOf(1);
19             String s = cin.next();
20             System.out.println(a + " " + s);
21             for (int i = s.length() - 1; i >= 0; i--, idx = idx.multiply(BigInteger.valueOf(a))) {
22                 char t = s.charAt(i);
23                 //8 62 2 abcdefghiz  System.out.println("char t = " + t);
24                 int add;
25                 if (t >= '0' && t <= '9') {
26                     add = t - '0';
27                 } else if (t >= 'A' && t <= 'Z') {
28                     add = t - 'A' + 10;
29                 } else {
30                     add = t - 'a' + 36;
31                 }
32                 r = r.add(idx.multiply(BigInteger.valueOf(add)));
33             }
34 
35             //  System.out.println(r.toString());
36             String res = "";
37             idx = BigInteger.valueOf(b);
38             if (s.equals("0")) {
39                 System.out.println(b + " 0");
40             } else {
41                 while (r.compareTo(BigInteger.ZERO) > 0) {
42                     BigInteger ad[];
43                     ad = r.divideAndRemainder(idx);
44                     r = ad[0];
45                     int t = ad[1].intValue();
46                     char ch;
47                     if (t >= 0 && t <= 9) {
48                         ch = (char) (t + '0');
49                     } else if (t >= 10 && t <= 35) {
50                         ch = (char) (t - 10 + 'A');
51                     } else {
52                         ch = (char) (t - 36 + 'a');
53                     }
54                     res = ch + res;
55                 //     System.out.println("ad[0]= "+ad[0]);
56                 //     System.out.println("ad[1]= "+ad[1]);
57                 }
58                 System.out.println(b + " " + res);
59             }
60             System.out.print('\n');
61         //System.out.println("res = "+res);
62         }
63     }
64 }
65 
66 


posted on 2009-09-10 21:32 schindlerlee 阅读(1047) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理