首页 > 试题广场 >

字典序

[编程题]字典序
  • 热度指数:15774 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
给定整数n和m, 将1到n的这n个整数按字典序排列之后, 求其中的第m个数。
对于n=11, m=4, 按字典序排列依次为1, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 因此第4个数是2.
对于n=200, m=25, 按字典序排列依次为1 10 100 101 102 103 104 105 106 107 108 109 11 110 111 112 113 114 115 116 117 118 119 12 120 121 122 123 124 125 126 127 128 129 13 130 131 132 133 134 135 136 137 138 139 14 140 141 142 143 144 145 146 147 148 149 15 150 151 152 153 154 155 156 157 158 159 16 160 161 162 163 164 165 166 167 168 169 17 170 171 172 173 174 175 176 177 178 179 18 180 181 182 183 184 185 186 187 188 189 19 190 191 192 193 194 195 196 197 198 199 2 20 200 21 22 23 24 25 26 27 28 29 3 30 31 32 33 34 35 36 37 38 39 4 40 41 42 43 44 45 46 47 48 49 5 50 51 52 53 54 55 56 57 58 59 6 60 61 62 63 64 65 66 67 68 69 7 70 71 72 73 74 75 76 77 78 79 8 80 81 82 83 84 85 86 87 88 89 9 90 91 92 93 94 95 96 97 98 99 因此第25个数是120…

输入描述:
输入仅包含两个整数n和m。
数据范围:
对于20%的数据, 1 <= m <= n <= 5 ;
对于80%的数据, 1 <= m <= n <= 10^7 ;
对于100%的数据, 1 <= m <= n <= 10^18.


输出描述:
输出仅包括一行, 即所求排列中的第m个数字.
示例1

输入

11 4

输出

2


  1. import java.util.Scanner;
  2. public class Main {
  3.     //包括自己的节点数,即字典树的数字pre分支的所有节点数
  4.     //pre分支的深度为1的孩子:pre*10,pre*10+1,pre*10+2,...,pre*10+10-1,孩子数为10
  5.     //pre分支的深度为2的孩子:pre*100,pre*100+1,pre*100+2,...,pre*100+100-1,孩子数为100
  6.     private static long getAllChildCount(long pre,long n){
  7.         long count=1;//计算自己的节点数
  8.         //pre*10是pre深度为1的第一个孩子的数字,pre*100是pre深度为2的第一个孩子的数字
  9.         //pre*p<=n,即检测在第某个深度下,是否存在该深度的第一孩子,若该孩子不存在,在该深度下,肯定不会有其他孩子存在。若存在循环累加计算
  10.         //pre*p+p-1,即pre的某个深度下,最右边的孩子的数字(不考虑n情况)
  11.         for(long p=10;pre*p<=n;p*=10){
  12.             if(pre*p+p-1<=n) count+=p;
  13.             else count+=(n-pre*p+1);//这里+1是因为[1,n],n也要算进去
  14.         }
  15.         return count;
  16.     }
  17.     
  18.     //该函数是寻找第m个数是什么,通过节点数进行分支选择
  19.     //如果该分支的节点数较m来说过少,那么第m的数肯定不在这个分支上,检测下个分支
  20.     //检测下个分支的时候,不是在这个分支寻找第m的数字,而是寻找第(m-上一个分支的节点数)的数
  21.     //如果分支的节点数较m来说过多, 选择该分支,如果m为1,那么答案就是这个分支的根节点
  22.     private static long find(long n,long m){
  23.         long ans=1;//默认在第1分支寻找
  24.         while(m!=0){
  25.             long v=getAllChildCount(ans, n);//获得该分支的节点数
  26.             if(v<m) {
  27.                 m-=v;
  28.                 ans++;//检测下一个分支
  29.                 continue;
  30.             }
  31.             //找到了
  32.             if(m==1) break;
  33.             //在该分支的孩子分支上,从第一孩子开始寻找
  34.             ans*=10;
  35.             m--;//是在孩子分支的第m-1个上
  36.         }
  37.         return ans;
  38.     }
  39.     //注意是long
  40.     public static void main(String[] argv){
  41.         Scanner input=new Scanner(System.in);
  42.         long n=input.nextLong();
  43.         long m=input.nextLong();
  44.         System.out.println(find(n,m));    
  45.     }
  46. }

发表于 2018-11-07 16:01:30 回复(0)

import java.util.Arrays;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
int[] array=new int[n];
String[] str=new String[n];
for(int i=0;i<n;i++) {
array[i]=i+1;
str[i]=Integer.toString(array[i]);
}
Arrays.sort(str);
//compareNum(array);
System.out.println(str[m-1]);
}

}
直接用字符串的比较排序即可,复杂度很低,不知道为啥运算超时

发表于 2018-06-22 11:46:46 回复(4)

学习别人的代码:和递归树类似,字典序也可以看成树形结构(逻辑上),分别从两个角度寻找第m个数(子节点,兄弟节点)。

import java.util.*;

public class Main {
    long getCountByPrifx(long currentPrifxCount,long n){
        long count = 1;
        long p = 10;
        for(;currentPrifxCount * p <= n; p *= 10){
            if (currentPrifxCount * p - 1 + p < n) {
                count += p;
            }else {
                count += n - currentPrifxCount * p + 1;
            }
        }
        return count;
    }

    long getNumFromDict(long n,long mth){
        long ans = 1;
        long count ;
        while (mth != 0){
            //当前节点开头的数字个数
            count = getCountByPrifx(ans,n);
            if (count >= mth){
                //count >= mth && mth != 0 的含义是当前节点不是要找的点,也不在兄弟k开头的数字中,向子节点开头的数字遍历
                mth--;
                if (mth == 0) break;
                //以当前 0号子节点为前者继续找
                ans *= 10;

            }else {
                //兄弟节点中找
                mth -= count;
                ans++;
            }
        }
        return ans;
    }

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            long n = sc.nextLong();
            long m = sc.nextLong();
            System.out.println(new Main().getNumFromDict(n,m));
        }
        sc.close();
    }
}
发表于 2018-01-27 16:16:00 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            long n =sc.nextLong();
            long m =sc.nextLong();

            long i = 1;//从1开始数
            m--;
            while(m!=0){
                long num = 0;
                long start = i,end = i+1;
                while(start<=n){
                    num += Math.min(n+1,end)-start;
                    start*=10;
                    end*=10;
                }
                if(num>m){
                    i *= 10;
                    m--;
                }else{
                    m-=num;
                    i++;
                }

            }

            System.out.println(i);



        }

    }



} 

编辑于 2017-08-22 16:53:24 回复(3)