一年多没有刷过算法题了,已经不打算找计算机类工作了,但是思来想去,还是继续找吧。
1. 字符串最后一个单词的长度

public static void main(String[] args) {
            Scanner in =new Scanner(System.in);
            while(in.hasNextInt()){
                String item=in.nextLine();
                System.out.println(item.length()-1-item.lastIndexOf(" "));
            }
        }
 
总长度-1-" "后的位置下标,即最后单词的长度
2.计算某字符出现的次数

public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String in1=in.nextLine().toLowerCase();
        String in2=in.nextLine().toLowerCase();
        String str=in1.replaceAll(in2,"");
        System.out.println(in1.length()-str.length());
        }
 
将字符串全部变成小写的,用字符串2去替代1中并删除,1的长度减去新替代的长度,就是出现的次数。
3.明明的随机数

public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            int num=sc.nextInt();
            TreeSet set=new TreeSet();
            for(int i=0;i<num;i++){
                set.add(sc.nextInt());
            }
            Iterator iterator=set.iterator();
            while(iterator.hasNext()){
                System.out.println(iterator.next());
            }
        }
 
Treeset不能重复存储数字,且按大小输出。集合需要迭代器,迭代器输出。













![[BT]BUUCTF刷题第8天(3.26)](https://img-blog.csdnimg.cn/direct/4828c922db9d45dc856e8efacf2dc487.png)


![[HackMyVM]靶场Crossbow](https://img-blog.csdnimg.cn/direct/25827a67260046e886294dc1684a1748.png)


