1. 字典排序
给定一个整数n,给定一个整数m,将1~n个整数按字典顺序进行排序,返回排序后第m个元素。n最大可为5000000。字典排序的含义为:从最高位开始比较。1开头的数字排在最前面,然后是2开头的数字,然后是3开头的数字……最高位相同的数字,按同样的逻辑比较次高位……以此类推。
例:给定整数为n=13,m=5,那么字典排序结果为: [1,10,11,12,13,2,3,4,5,6,7,8,9] ,程序最终输出为13。
解释:用treeset一次AC
- import java.util.*;
-
- public class Main {
-
- public static void main(String[] args){
- Scanner in = new Scanner(System.in);
- while(in.hasNext()){
- int m=in.nextInt();
- int n=in.nextInt();
- Set<String> set = new TreeSet<String>();
- for(int i=1;i<=n;i++){
- set.add(Integer.toString(i));
- }
- Iterator it = set.iterator();
- String[] strArr = (String[]) set.toArray(new String[set.size()]);
- System.out.println(strArr[m-1]);
- }
- }
- }
2. 字符串处理
一个字符串,含有字母数字和特殊符号等,按照以下要求写一个转换函数
1)只保留小写字母,过滤掉其他字符,
2)对过滤结果按照下述规则进行映射转换:(0<right<=25)
例如若right = 2 :
转换前->转换后
a->c
b->d
c->e
…
x->z
y->a
z->b
例如1 输入字符串:"difg123" right =3, 则最后输出为” glij”
例如2 输入字符串:"yz" 右偏移为3, 则最后输出为” bc”
一次AC,没啥说的
- import java.util.*;
-
- public class Main {
-
- public static void main(String[] args){
- Scanner in = new Scanner(System.in);
- while(in.hasNext()){
- String s = in.nextLine();
- int r = in.nextInt();
- StringBuffer ret = new StringBuffer();
- for(int i=0;i<s.length();i++){
- if(s.charAt(i)>='a'&&s.charAt(i)<='z'){
- ret.append((char)(((s.charAt(i)+r)-'a')%26+'a'));
- }
- }
- System.out.println(ret.toString());
- }
- }
- }
3. 乘坐电梯
一栋大楼共有M层(M >= 2 ),且只有一个电梯,该电梯最大载重为K。现在有N个人排队坐电梯上楼,且每个人在队中位置不能改变。队中每个人体重为weight [w(0), w(1), w(2) ….w(N-1) ],且满足 w(i)<K,保证每个人都可以上楼。每个人目的楼层为destination [d(0), d(1), d(2) ….d(N-1)],且2<=d(i)<=M ,求该电梯上楼过程中一共停留了多少次(不包括起始的楼层第1层)。计算中不考虑人员下楼用电梯。
举例1. 有两个人 N=2,楼层M=3,电梯载重K = 100,每个人体重weight[2] = {80, 90},目的楼层destination[2] = {3, 3}, 则
第一次电梯载1人上楼,停1次在3楼,第二次电梯载1人上楼,停2次在3楼,
电梯共启动2次,上升中共停2次,程序输出 2
举例2. 有两个人 N=2,楼层M=3,电梯载重K = 180,每个人体重weight[2] = {80, 90},目的楼层destination[2] = {3, 3}, 则
第一次电梯载2人上楼,停1次在3楼,
电梯共启动1次,上升中共停1次,程序输出 1
输入
输入占五行,每行内容依次为人数,楼层数,电梯载重,人员体重数组(空格隔开),人员目的楼层数组(空格隔开)。
输出
数字(代表电梯停留次数)
样例输入
2
3
100
80 90
3 3
样例输出
2
-
- import java.util.*;
-
- public class Main {
-
- public static void main(String[] args){
- Scanner in = new Scanner(System.in);
- while(in.hasNext()){
- int N = in.nextInt();
- int M = in.nextInt();
- int K = in.nextInt();
- int[] w = new int[N];
- for(int i=0;i<N;i++){
- w[i]=in.nextInt();
- }
- int[] d = new int[N];
- for(int i=0;i<N;i++){
- d[i]=in.nextInt();
- }
- int count=0;
- int tempsum=0;
- Set<Integer> set = new HashSet<Integer>();
- for(int i=0;i<N;i++){
- tempsum += w[i];
- if(tempsum<K){
- set.add(d[i]);
- if(i==N-1){
- count += set.size();
- }
- }
- else{
- count += set.size();
- set.clear();
- tempsum=0;
- i--;
- }
- }
- System.out.println(count);
- }
- }
- }