求最简单算法:字符串处理问题:
条件:
      String temp="01011,0201012,030101013";
      String temp2="01X,01011,02X,0201X,0201012,03X,0301X,030101X,030101013";
规律:
     字串1中的每人元素分别左取它的(长度-3)或(长度-3-2的倍数)后面与"X"组成一个新字串,
     把这个新的字串加到原来的字串之中.
要求:
    如何以最简单的算法,由temp求出temp2。

解决方案 »

  1.   

    楼主,你的temp2里03x是哪个规律来的,
    把你的规律说说清楚啊
      

  2.   

    TO believefym(feng):
        规律:
         字串1中的每人元素分别左取它的(长度-3-2n)后面与"X"组成一个新字串,
         把这个新的字串加到原来的字串之中.(如:对030101013来说: n>=0,n<3)
      

  3.   

    import java.util.*;public class GetTemp2 {
    static String temp="01011,0201012,030101013333333";
    static String temp2 = new String(); public static void main(String[] args) {
    StringTokenizer st = new StringTokenizer(temp.trim(),",");
    while(st.hasMoreTokens()){
    String str = st.nextToken();
    append(str);
    temp2 += str+",";
    }

    temp2 = temp2.substring(0,temp2.length()-1);
    System.out.println(temp2);

    }

    static void append(String str){
    for(int i=(str.length()-3)/2; i>=0; i--){
    if(str.length()-3-2*i>0){
    temp2 += str.substring(0,str.length()-3-2*i)+"x,";
    }
    }
    } }
      

  4.   

    自己随便写的,没有楼上的简单,
    --------------------------------
    String temp="01011,0201012,030101013";
            StringBuffer temp2 = new StringBuffer();
            int index = 2;
            while (index < temp.length())
            {
                if ((temp.charAt(index) == '0'))
                {
                    if (temp2.length() < 1)
                    {
                        temp2.append(temp.substring(0, index)).append("X");
                    }
                    else
                    {
                        temp2.append(",").append(temp.substring(0, index)).append("X");
                    }
                    ++index;
                }
                else if ((temp.charAt(index) == ',') || (temp.length() - 1 == index))
                {
                    if (temp2.length() < 1)
                    {
                        temp2.append(temp.substring(0, index));
                    }
                    else
                    {
                        temp2.append(",").append(temp.substring(0, index));
                    }
                    temp = temp.substring(index+1);
                    index = 2;
                }
                else if (index < temp.length())
                {
                    ++index;
                }
            }