日期:2014-05-20  浏览次数:20694 次

算数题
用A,B ,C 来表示一个三进制的书,例如A十进制的0,以此类推A=0,B=1,C=2。BA=3,BC=5,BAAA=27最大位6位数,
也就是CCCCCC,用java完成此运算。

------解决方案--------------------
Java code
        String s = Integer.toString(728, 3);
        byte[] bytes = s.getBytes();
        for(int i = 0; i < bytes.length; i++){
            System.out.println(bytes[i]);
            bytes[i] = (byte)(bytes[i] + 17);
        }
        System.out.println(new String(bytes));

------解决方案--------------------
for example
Java code
public class csdn {
    public static void main(String[] args) throws Throwable {
        String s = "222222";
        System.out.println(Integer.parseInt(s, 3));
        System.out.println(toString(728, 3));
    }

    public static String toString(int n, int radix) {
        char[] dig = {'A', 'B', 'C'};
        if (radix > dig.length) {return "100000";}
        String s = Integer.toString(n, radix);
        StringBuilder buf = new StringBuilder();
        for (char c : s.toCharArray()) {
            buf.append(dig[c-'0']);
        }
        return buf.toString();
    }
}

------解决方案--------------------
String s = Integer.toString(728, 3);
byte[] bytes = s.getBytes();
for(int i = 0; i < bytes.length; i++){
System.out.println(bytes[i]);
bytes[i] = (byte)(bytes[i] + 17);
}
System.out.println(new String(bytes));

------解决方案--------------------
[code=Java][/code]
System.out.println("输入要处理的数字(最大值728):");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
Integer co = Integer.parseInt(s);
if(co <= 728){
String ss = Integer.toString(co, 3);
byte[] by = ss.getBytes();
for(int i=0; i<by.length; i++){
by[i] = (byte)(by[i] + ('A' - '0'));
}
System.out.println(new String(by));
}else
System.out.println("请输入不大于728的数!");
------解决方案--------------------
Java code

System.out.println("输入要处理的数字(最大值728):");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s = br.readLine();
        Integer co = Integer.parseInt(s);
        if(co <= 728){
            String ss = Integer.toString(co, 3);
            byte[] by = ss.getBytes();
            for(int i=0; i<by.length; i++){
                by[i] = (byte)(by[i] + ('A' - '0'));
            }
            System.out.println(new String(by));
        }else
            System.out.println("请输入不大于728的数!");