• Java刷题day22——牛客编程题


    1.猴子分桃

    题目描述:老猴子辛苦了一辈子,给那群小猴子们留下了一笔巨大的财富——一大堆桃子。老猴子决定把这些桃子分给小猴子。
    第一个猴子来了,它把桃子分成五堆,五堆一样多,但还多出一个。它把剩下的一个留给老猴子,自己拿走其中的一堆。
    第二个猴子来了,它把桃子分成五堆,五堆一样多,但又多出一个。它把多出的一个留给老猴子,自己拿走其中的一堆。
    后来的小猴子都如此照办。最后剩下的桃子全部留给老猴子。
    这里有n只小猴子,请你写个程序计算一下在开始时至少有多少个桃子,以及最后老猴子最少能得到几个桃子。

    输入描述:
    输入包括多组测试数据。
    每组测试数据包括一个整数n(1≤n≤20)。
    输入以0结束,该行不做处理。
    输出描述:
    每组测试数据对应一行输出。
    包括两个整数a,b。
    分别代表开始时最小需要的桃子数,和结束后老猴子最少能得到的桃子数。

    解题思路:
    因为每次分5堆都会多出来1个,所以我们借给猴子们4个,以致每次都可以刚好分成5堆. 并且,每次给老猴子的桃子都不在我们借出的那4个中,这样最后减掉4就可以得到结果。

    1. 假设最初由x个桃子,我们借给猴子4个,则此时有x+4个, 第一个猴子得到(x+4)/5,剩余(x+4)(4/5)个
    2. 第二个猴子分完后剩余(x+4) (4/5)^2个
    3. 第三个猴子分完后剩余(x+4) (4/5) ^3个
    4. 依次类推,第n个猴子分完后剩余(x+4)(4/5)^ n
    5. 要满足最后剩余的为整数,并且x最小,则当 x+4=5^n时,满足要求;
    6. 此时,x=5^n - 4; 老猴子得到的数量为:(x+4)*(4/5)^n + n - 4
      = 4^n + n - 4 最后的 +n是因为每个小猴子都会多出一个给老猴子,-4是还了借的4个
      代码:
    import java.util.*;
    public class Main{
        public static void main(String[] args){
            Scanner sc = new Scanner(System.in);
            while(sc.hasNext()){
                int n = sc.nextInt();
                if(n == 0){
                    break;
                }
                long a = (long)(Math.pow(5,n)-4);
                long b = (long)(Math.pow(4,n)+n-4);
                System.out.println(a + " " +b);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    2. 反转部分单向链表

    给定一个单链表,在链表中把第 L 个节点到第 R 个节点这一部分进行反转。
    思路:先找到待反转区间的前驱结点prev,cur结点指向待反转的第一个结点,将cur.next先从链表中删除,然后将删除的结点插入到prev的next,此时cur的next就恰好变成了cur的前驱。
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    import java.util.*;
    public class Main{
        static class Node{
            int val;
            Node next;
            public Node(int val){
                this.val = val;
            }
        }
        public static void main(String[] args){
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            sc.nextLine();
            String valueNode = sc.nextLine();
            String[] nodes = valueNode.split(" ");
            Node dummyHead = new Node(-1);
            Node tail = dummyHead;
            for(int i =0;i<n;i++){
                Node node = new Node(Integer.parseInt(nodes[i]));
                tail.next = node;
                tail = node;
            }
            String part = sc.nextLine();
            int left = Integer.parseInt(part.split(" ")[0]);
            int right = Integer.parseInt(part.split(" ")[1]);
            Node newHead = reversePartList(dummyHead.next,left,right);
            while( newHead != null ){
                System.out.print(newHead.val+" ");
                newHead = newHead.next;
            }
        } 
        public static Node reversePartList(Node head,int left ,int right){
            Node dummyHead = new Node(-1);
            dummyHead.next = head;
            Node prev = dummyHead;
            for(int i = 1;i<left;i++){
                prev = prev.next;
            }
            Node cur = prev.next;
            for(int i =left ;i<right;i++){
                Node third = cur.next;
                cur.next = third.next;
                third.next = prev.next;
                prev.next = third;
            }
            return dummyHead.next;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
  • 相关阅读:
    Linux驱动开发(十六)---块设备驱动
    redis基础和原理全覆盖
    熟悉HotSpot中的对象
    【大厂招聘试题】__嵌入式开发工程师_2023届“联想”_1
    大数据Flink(七十一):SQL的时间属性
    Google Earth Engine-04(GEE中的JavaScript和原始JavaScript的异同和联系)
    代码随想录 Day38 完全背包问题 LeetCode T70 爬楼梯 T322 零钱兑换 T279 完全平方数
    【已解决】windows电脑连蓝牙耳机总是断断续续?
    Java基础:代理
    合规运营必备资质——ICP申请指南
  • 原文地址:https://blog.csdn.net/m0_52322019/article/details/126099115