实现一个java程序,
对于一个String变量如:str="A0B58B",其最大值为“ZZZZZZ”。
实现一个函数:
对这个string变量进行累加的功能,例如对str="A0B58B"加1后为“A0B58C",再加1为A0B58D,如此加法,每位最大为Z,超了对高位加一,自己变为0,一直累加到最大值为止。
 
可能用到java中的位的操作,我不熟悉。所以麻烦各位高手支招,最好有代码。。谢谢大家!

解决方案 »

  1.   

    应该是36进制的吧?
    先把目前的str="A0B58B"转化为10进制,之后累加,再之后转化为36进制。
      

  2.   

    很简单呀
    String increment(String s){
        return  Long.toString(Long.praseLong(s,36)+1,36); //这里没有考虑左边对齐问题
    }
      

  3.   

    System.out.println(Long.toString(Long.parseLong("A2FE2C",36)+1,36).toUpperCase());
      

  4.   

    上面两位已经使用java.lang包中的Long类解决了
      

  5.   

    public class Test {
    private String[] strings = new String[] {
    "0", "1", "2","3","4","5","6","7","8", "9",
    "A", "B", "C", "D","E","F","G","H","I","J",
    "K","L","M","N","O","P","Q","R","S","T","U",
    "V","W","X","Y","Z"
    };
    public static void main(String[] args) {
    Test test = new Test();
    System.out.println(test.getResult("A0BZZZ"));
    } public String getResult(String string) {
    String lastString = string.substring(string.length()-1, string.length());
    int index = findIndex(lastString);
    if (index == -1) return null;
    boolean next = index + 1 >= strings.length;
    if (!next) {
            string = string.substring(0, string.length()-1) + strings[index+1];
    return string;
    }
    String resultString = getResult(string.substring(0, string.length()-1));
    if (resultString.length() != string.length()) return resultString + "0";
    return resultString;
    } private int findIndex(String string) {
    for (int i = 0; i < strings.length; i++) {
    if (strings[i].equals(string)) return i;
    }
    return -1;
    }
    }非JAVA.LANG包的解决方案
      

  6.   

    厉害,但是能自己实现的,(见笑了)
    public class Turn { public static void main(String[] args) {
    String s = "A0B58B";
    char sChar[] = s.toCharArray();
    int number[] = new int[s.length()];
    int sum = 0;
    for (int i = 0; i < number.length; i++) {
    number[i] = getNumber(sChar[i]);
    sum += number[i] * Math.pow(36, number.length - i - 1);
    }
    sum += 1;
    int temp = 0;
    for (int i = 0; i < sChar.length; i++) {
    temp = sum % 36;
    sum = sum / 36;
    sChar[sChar.length - i - 1] = getChar(temp);
    }
    System.out.println(sChar);
    System.out.println(increment("A0B58B"));
    } private static char getChar(int temp) {
    if (temp <= 9)
    return (char) (temp + 48);
    else
    return (char) (temp + 55);
    } private static int getNumber(char char1) {
    switch (char1) {
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
    return char1 - 48;
    default:
    return char1 - 55;
    }
    }
      

  7.   

    你的实现的不好,看这个public static long convertToDec(String str) throws Exception{
    str=str.trim().toUpperCase();
    if(str.length()>6)
    throw new Exception("Too big");
    if(str.length()==0)
    throw new Exception("No value");
    if(!str.matches("[A-Za-z0-9]*"))
    throw new Exception("There are invalid character.");
    long value=0;
    for(int i=0;i<str.length();i++){
    char c=str.charAt(i);
    int t=c>=65?c-55:c-48;
    value=value+(long)(t*Math.pow(36,str.length()-i-1));
    }
    return value;
    } public static String convertToString(long number){
    char c;
    if(number>=36){
    long t=number%36;
    c=(char)(t<9?t+48:t+55);
    return convertToString(number/36)+c;
    }else{
    c=(char)(number<9?number+48:number+55);
    return ""+c;
    }
    } public static String inc(String str) throws Exception{
    long s=convertToDec(str)+1;
    if(s>=2176782336L)
    throw new Exception("Result too big");
    return convertToString(s);
    }