有如下数据:
展开层数
1
2
2
3
3
3
3
2
2
2
1
2
3
2
表征上下两行之间的关系:小数是大数的直接父项,大数是小数的直接子项。现在要将数据分层次,直到没有子项为止。如:展开层数          层次
1                 1 
2                 1.1
2                 1.2
3                 1.2.1
3                 1.2.2 
3                 1.2.3
3                 1.2.4
2                 1.3
2                 1.4                 
2                 1.5
1                 2
2                 2.1
3                 2.1.1
2                 2.2求该算法。

解决方案 »

  1.   

    展开层数 层次
    1               1  
    2               1.1
    2               1.2
    3               1.2.1
    3               1.2.2  
    3               1.2.3
    3               1.2.4
    2               1.3
    2               1.4   
    2               1.5
    1               2
    2               2.1也就是说,对每行数据添加a.b.c...  表示出该数据在所有关系中的层次
    3 2.1.1
    2 2.2
      

  2.   

    额,写了下,应该可以满足需求了,不足的地方曼自己改吧改吧啦public class Test{
    public static void main(String[] args) {
    int[] nums = {1,2,2,3,3,3,3,2,2,2,1,2,3,2};
    String[] strs = new String[nums.length];
    strs[0]="1";
    for(int i=1;i<nums.length;i++){
    if(nums[i]>nums[i-1]){
    String str = strs[i-1];
    strs[i]=str+"."+"1";
    }else if(nums[i]==nums[i-1]){
    String str = strs[i-1];
    char last = str.charAt(str.length()-1);
    strs[i]=str.substring(0,str.length()-1)+(char)(last+1);
    }else{
    for(int j=i-1;j>=0;j--){
    if(nums[j]==nums[i]){
    String str = strs[j];
    char last = str.charAt(str.length()-1);
    strs[i]=str.substring(0,str.length()-1)+(char)(last+1);
    break;
    }
    }
    }
    }
    for(int i=1;i<strs.length;i++){
    System.out.println(strs[i]);
    }
    }
    }输出:
    1.1
    1.2
    1.2.1
    1.2.2
    1.2.3
    1.2.4
    1.3
    1.4
    1.5
    2
    2.1
    2.1.1
    2.2
      

  3.   

    忘记注释了思路如下:1、初始化第一个为1(肯定是1,不是1的话你的目录树就错了)
    2、对于后面的每一个,和前面一个的值比较,分三种情况:
      a、比前面一个大,说明是前面节点的第一个子节点,在后面加上“.1”
      b、和前面一个相同,说明是前面节点并列的节点,把前面节点的最后一位拿出来,加1替换上去
      c、比前面一个小,就倒着遍历找到第一个和该节点值相同的,说明两节点并列
         把这个节点的值拿出来,最后一位截取出来加1替换上去
      

  4.   

    import java.util.Arrays;
    public class test {
       
        public static void main(String args[]) {
         int source[] = {1,2,2,3,3,3,3,2,2,2,1,2,3,2};
         int countCol = max(source);
         int dest[][] = new int[source.length][countCol];
         int footer[] = new int[countCol];
         for (int i = 0;i < source.length;i++){
         for (int j = 0;j < source[i];j++){
         if (j == source[i] - 1) {
         footer[j]+=1;
         if (j < countCol-1)
         resetArrayAfterIndex(footer, j+1);
         }
         dest[i][j] = footer[j];
         }
         }
         for (int i = 0; i < source.length; i ++){
         System.out.print(source[i]+"   ");
         for (int j = 0; j < countCol; j ++){
         if (dest[i][j] > 0){
         System.out.print(dest[i][j]+" ");
         }
         }
         System.out.print("\n");
         }   
        }
        public static int max(int aArray[]){
         int anotherArray[] = aArray.clone();
         Arrays.sort(anotherArray);
         return anotherArray[anotherArray.length-1];    
        }
        
        public static void resetArrayAfterIndex(int aArray[], int index){
         for (int i = index; i<aArray.length;i++){
         aArray[i] = 0;
         }
        }
    }
      

  5.   

        int[] array = new int[] {
            1, 2, 2, 3, 3, 3, 3, 2, 2, 2, 1, 2, 3, 2
        };
        int[] index = new int[10]; // 最大级数有几层?
        int lastDepth = 0; // 上一次是第几层目录
        for (int i = 0; i < array.length; i++) {
          int depth = array[i] - 1;
          System.out.print(array[i]);
          System.out.print(' ');
          for (int j = 0; j < depth; j++) {
            System.out.print(index[j]);
            System.out.print('.');
          }
          if (lastDepth >= depth) {
            index[depth]++;
          } else if (lastDepth < depth) {
            index[depth] = 1;
          }
          System.out.println(index[depth]);
          lastDepth = depth;
        }
      

  6.   

    6F的代码一点没看懂,没必要搞那么复杂,好像还用到了排序
    4F的大意好像和我一样,但是应该直接放一个int[]而不是通过string来计数
      

  7.   

    假设我的int[] index 现在是[3,4,2,1,6...]
    就是说,现在书正翻到第三部、第四篇、第二章、第一节、第六小节....,
      

  8.   

    接受指正,而且确实有问题,改了一下
    import java.util.Arrays;
    public class test {
       
        public static void main(String args[]) {
         int source[] = {4,21,2,3,3,3,4,22,12,23,11,22,31,2};
         int countCol = max(source);
         int dest[][] = new int[source.length][countCol];
         int footer[] = new int[countCol];
         for (int i = 0;i < source.length;i++){
         System.out.print(source[i]+"\t");
         for (int j = 0;j < source[i];j++){    
         if (j == source[i] - 1) {
         footer[j]+=1;
         if (j < countCol-1)
         resetArrayAfterIndex(footer, j+1);
         }
         else if (footer[j] == 0)
         footer[j] = 1;
         dest[i][j] = footer[j];
         if (dest[i][j] > 0)
         System.out.print(dest[i][j]+" ");
         }
         System.out.print("\n");
         }
        }
        public static int max(int aArray[]){
         int max = 0;
         for (int i:aArray)
         if (i > max)
         max = i;   
         return max;
        }
        
        public static void resetArrayAfterIndex(int aArray[], int index){
         for (int i = index; i<aArray.length;i++){
         aArray[i] = 0;
         }
        }
    }
      

  9.   

    public class test { public static void main(String[] args) {
    // int[] nums = { 1, 2, 2, 3, 3, 3, 3, 2, 2, 2, 1, 2, 3, 2 };
    int[] nums = { 1, 2, 2, 3, 3, 3, 3, 2, 2, 2, 1, 2, 3, 4, 4, 5, 5, 5, 4,
    3, 3, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 6, 6, 6 }; int indeOne = 1;
    int max = nums[0];
    String result = ""; for (int i = 1; i < nums.length; i++) {
    if (max < nums[i]) {
    max = nums[i];
    }
    } int indexs[] = null;
    for (int i = 0; i < nums.length; i++) {
    if (nums[i] == 1) {
    indexs = new int[max + 1];
    indexs[nums[i]] = indeOne++;
    System.out.println(indexs[nums[i]] + "");
    } else {
    indexs[nums[i]]++;
    result = indexs[1] + "";
    for (int k = 2; k <= nums[i]; k++) {
    result = result + "." + indexs[k];
    }
    System.out.println(result);
    }
    } }}结果:
    1
    1.1
    1.2
    1.2.1
    1.2.2
    1.2.3
    1.2.4
    1.3
    1.4
    1.5
    2
    2.1
    2.1.1
    2.1.1.1
    2.1.1.2
    2.1.1.2.1
    2.1.1.2.2
    2.1.1.2.3
    2.1.1.3
    2.1.2
    2.1.3
    2.2
    3
    3.1
    3.1.1
    3.1.1.1
    3.1.1.1.1
    3.1.1.1.1.1
    3.1.1.1.1.1.1
    3.1.1.1.1.1.1.1
    3.1.1.1.1.1.1.1.1
    3.1.1.1.1.1.1.1.1.1
    3.1.1.1.1.1.1.1.1.1.1
    3.1.1.1.1.2
    3.1.1.1.1.3
    3.1.1.1.1.4
      

  10.   


    CSDN高手如云所以很快就解决了呵呵,兄台算法也很精妙啊,下次早点吧! 同谢