算法特训,AB5 .点击消除BC.149简写单词牛客.除2!牛客.Fibonacci数列

目录

AB5 .点击消除

BC.149简写单词

牛客.除2!

牛客.Fibonacci数列


AB5 .点击消除


点击消除,类似于括号匹配a(b[b]a){c{d,这种,利用栈去消除,这样正好可以处理,假如相同就不进栈,同时还要出栈。注意我们这么搞完他是一个逆序,我们需要一个reverse()

 public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //aba,aab
        String a = scanner.nextLine();
        char[] b = a.toCharArray();
        Stack<Character> c = new Stack<>();

        for (int i = 0; i < b.length; i++) {
            if (c.isEmpty()) {
                c.push(b[i]);
            } else {
                if (b[i] == c.peek()) {
                    c.pop();
                } else c.push(b[i]);
            }
        }
        StringBuffer ret = new StringBuffer();
        if (c.isEmpty()){
            System.out.println(0);
        }else {
            while (!c.isEmpty()) {
                ret.append(c.pop());
            }
            ret.reverse();
            System.out.println(ret.toString());
        }
    }
}

BC.149简写单词

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String a1=in.nextLine();
        String ret="";
        String []a=a1.split(" ");
        int b=a.length;
        for(int i=0;i<b;i++){
            if(a[i].charAt(0)>='a'){
              ret+=(char)(a[i].charAt(0)-32);
            }else{
                ret+=a[i].charAt(0);
            }
        }
        System.out.println(ret);
    }
}

牛客.除2!

我们把偶数都放在堆里面,注意⚠️也要看数字的范围10^9我们需要,

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();
//逆序的大根堆
        PriorityQueue<Integer> a = new PriorityQueue<>((c,d)->{
            return d-c;
        });
        ArrayList<Integer> ret=new ArrayList<>();
        for (int i = 0; i < n; i++) {
            long x=scanner.nextLong();
            if(x%2==0){
//偶数放到堆里面
                a.add((int)x);
            }else
//奇数放到ArrayList里面
                ret.add((int) x);
        }
        long count = 0;
        while (k > 0 && !a.isEmpty()) {
                int t = a.poll();
                if(t%2==0){
                a.add(t / 2);
                    k--;}
//有可能10/2=5 然后把5放进来之后,5并没有修改
                else{
                    ret.add(t);
                }

        }
        for(int i=0;i<ret.size();i++)
            count += ret.get(i);
            while (!a.isEmpty()){
                count+=a.poll();
            }
        System.out.println(count);
    }   
    }

牛客.Fibonacci数列

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
//假如不用集合也可以,但是数组是没有集合好的,毕竟数组的长度你是无法确定的
        ArrayList<Integer>dp = new ArrayList<>();
        if (a == 1) {
            System.out.println(0);
        } else {
            dp.add(0);
            dp.add(1);
            int m = 0;
            int n = 1;
            int k = 1;
            int count = Integer.MAX_VALUE;
            int j = 2;
            while (dp.get(j - 1) < a) {
                dp.add(j, dp.get(j - 1) + dp.get(j - 2));
                j++;
            }
            for (int i = 2; i < dp.size(); i++) {
                m = dp.get(i - 2);
                n = dp.get(i - 1);
                k = dp.get(i);
                if (a < k && a > n) {
                    count = Math.min(count, Math.min(Math.abs(a - k), Math.abs(a - n)));
                } else if (a < n && a > m) {
                    count = Math.min(count, Math.min(Math.abs(a - m), Math.abs(a - n)));
                }
            }
            System.out.println(count);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狗哥不是甜妹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值