poj1312

java中StringBuffer类似于C++中的String,append函数可以在其后面添加字符,reverse可以反转。

java中BigInteger的intValue方法可以将其转为int。

View Code
import java.io.*;

import java.util.*;

import java.math.*;





public class Main {

    final static int maxl = 50;

    static String translate(String temp)

    {

        StringBuffer ret = new StringBuffer(temp);

        for (int i = ret.length() - 3; i > 0; i -= 3)

            ret.insert(i, ",");

        return ret.toString();

    }

    static void translateToNumber(String st)

    {

        BigInteger ret = new BigInteger("0");

        for (int i = 0; i < st.length(); i++)

        {

            ret = ret.multiply(BigInteger.valueOf(26));

            ret = ret.add(BigInteger.valueOf(st.charAt(i) - 'a' + 1));

//            System.out.println(ret);

        }

        System.out.printf("%-22s%s\n", st, translate(ret.toString()));

    }

    

    static void translateToString(String st)

    {

        BigInteger a = new BigInteger(st);

        StringBuffer ans = new StringBuffer("");

        while (!a.equals(BigInteger.ZERO))

        {

            BigInteger temp = a.mod(BigInteger.valueOf(26));

            if (temp.equals(BigInteger.ZERO))

            {

                ans.append("z");

                a = a.subtract(BigInteger.valueOf(26));

            }else

                ans.append((char) (temp.intValue() + 'a' - 1));

            a = a.divide(BigInteger.valueOf(26));

//            System.out.println(a);

        }

        ans.reverse();

        System.out.printf("%-22s%s\n", ans, translate(st));

    }

    

    public static void main(String[] args) throws FileNotFoundException

    {

//        Scanner cin = new Scanner(new FileInputStream("t.txt"));

        Scanner cin = new Scanner(System.in);

        while (true)

        {

            String st = cin.next();

            if (st.equals("*"))

                break;

            if (st.charAt(0) <= 'z' && st.charAt(0) >= 'a')

                translateToNumber(st);

            else

                translateToString(st);

        }

    }

}

 

你可能感兴趣的:(poj)