自学内容网 自学内容网

Day3--每日一练

今天继续练习算法题,又是充实的一天呢~

今日题目:1. 简写单词

                  2. dd爱框框

                  3. 数组除2操作,要求数组之和最小

题目一

简写单词,要求输入一系列单词,只输出每个单词的首字母并大写。

这里只用获取首字母即可,代码如下:

//简写单词
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            char ch = sc.next().charAt(0);
            if (ch >= 'a' && ch <= 'z'){
                System.out.println((char) (ch-32));
            }else {
                System.out.print(ch);
            }
        }
    }

题目二 

题目描述:dd爱框框

给一个数组,指定一个和,求数组中累加大于和的区间段,最终找出最先大于和的区间段

解决思路:利用同向指针--滑动窗口的模式进行解题,主要分为四步,右区间进窗口,判断条件,更新结果,左区间出窗口,最终返回区间的和。 

代码如下

 public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int k = in.nextInt();
        int[] arr = new int[n +1];
        for (int i = 1; i < n; i++) {
            arr[i] = in.nextInt();
        }
        int right = 1;
        int left = 1;
        int sum = 0;
        int retright = -1;
        int retleft = -1;
        int retLen = n;
        while (right <= n) {
            sum += arr[right];
            while (sum >= k) {
                if (right-left+1 > retLen) {
                    retleft = left;
                    retright = right;
                    retLen = right-left+1;
                }
                sum -= arr[left];
            }
            right++;
        }
        System.out.println(sum);
    }

但运行时发现,代码超时了!!!!!一时间也没啥头绪,最后发现需要自己编写一个快读函数调用,哎~,代码放在下面了……

class Read // 自定义快速读入
{
    StringTokenizer st = new StringTokenizer("");
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    String next() throws IOException 
    {
        while(!st.hasMoreTokens())
        {
            st = new StringTokenizer(bf.readLine());
        }
        return st.nextToken();
    }
    
    String nextLine() throws IOException 
    {
        return bf.readLine();
    }
    
    int nextInt() throws IOException 
    {
        return Integer.parseInt(next());
    }
    
    long nextLong() throws IOException 
    {
        return Long.parseLong(next());
    }
    
    double nextDouble() throws IOException 
    {
        return Double.parseDouble(next());
    }
}

题目三

题目描述:给一个数组,一共有 n 个数。 你能进行最多k次操作。每次操作可以进行以下步骤

选择数组中的一个偶数 ai,将其变成 ai/2。 现在你进行不超过k次操作后,让数组中所有数之和尽可 能小。请输出这个最小的和。

解题思路:创建一个大根堆,每次取堆顶元素(即数组中的最大值)进行除2操作,,是偶数就重新入堆,当堆为空或者操作结束,程序结束。主要是模拟上述情景的实现。

代码如下:

public static void main3(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int k = in.nextInt();
        //创建大根堆,堆顶元素减半,是偶数重新入堆
        PriorityQueue<Integer> heap = new PriorityQueue<>((a, b)->{
            return b-a;
        });
        long sum = 0;
        int  x = -1;
        for (int i = 0; i < n; i++) {
            x = in.nextInt();
            sum += x;
            if (x % 2 == 0) {
                heap.add(x);
            }
        }
        while (!heap.isEmpty() && k-- != 0) {
            int t = heap.poll() / 2;
            sum -= t;
            if (t % 2 == 0) {
                heap.add(t);
            }
        }
        System.out.println(sum);
    }

 


原文地址:https://blog.csdn.net/qq_74411357/article/details/140336528

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!