蓝桥算法训练__普及组.Day8

第一题:1843. 圆形牛棚 - AcWing题库

package 蓝桥算法训练__普及组.Day8;

import java.util.Scanner;

public class T1 {
    static int n;
    static int[] a = new int[1010];
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            a[i] = sc.nextInt();
        }
        int ans = (int) 1e9;// ans用来存答案
        int temp = 0;// temp用来存开每条门时 牛需要走的距离和
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                // 开第i条门 属于该门的牛不用加步数
                if (i != j) {
                    if (j > i){
                        temp += a[j] * (j - i);
                    } else {
                        temp += a[j] * (n - i + j);
                    }
                }
            }
            ans = Math.min(ans, temp);
//            System.out.println(ans + " " + i);
            temp = 0;
        }
        System.out.println(ans);
    }
}

第二题:32. 调整数组顺序使奇数位于偶数前面 - AcWing题库

package 蓝桥算法训练__普及组.Day8;

import java.util.Scanner;

/**
 * @author snippet
 * @data 2023-02-12
 * 32. 调整数组顺序使奇数位于偶数前面 - AcWing题库
 */
public class T2 {
    public static void reOrderArray(int[] array) {
        int left = 0, right = array.length-1;
        while (left < right) {
            // 奇数
            while (left < right && array[left] % 2 == 1) left++;
            // 偶数
            while (left < right && array[right] % 2 == 0) right--;
            if (left < right) {
                int temp = array[left];
                array[left] = array[right];
                array[right] = temp;
            }
        }
    }

    // 数据测试
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        reOrderArray(a);
        for (int i = 0; i < n; i++) {
            System.out.print(a[i] + " ");
        }
    }
}

第三题:1347. 双重回文 - AcWing题库

package 蓝桥算法训练__普及组.Day8;

import java.util.Scanner;
import java.util.Stack;

/**
 * @author snippet
 * @data 2023-02-12
 * 1347. 双重回文 - AcWing题库
 */
public class T3 {
    /**
     * 判断在2-10进制中是否回文
     * @param num
     * @param bs
     * @return
     */
    static boolean check(int num, int bs) {
        int t = num;
        // 栈用来存bs进制下的数字num
        Stack<Integer> stack = new Stack<>();
        while (t > 0) {
            stack.push(t % bs);
            t /= bs;
        }

        // 判断是否回文
        while (stack.size() > 0) {
            if (stack.peek() != num % bs) return false;
            stack.pop();
            num /= bs;
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();// 需要求n个数
        int m = sc.nextInt();// 从数字m开始
        int count = 0;
        m++;
        while (true) {
            if (count == n) {
                break;
            }
            // 这个temp用来记录数字m在2-10进制中满足回文的个数
            int temp = 0;
            for (int i = 2; i <= 10; i++) {
                if (check(m, i)) temp++;
            }
            // 满足两个及以上进制的数则直接输出
            if (temp >= 2) {
                System.out.println(m);
                count++;
            }
            m++;
        }
    }
}

第四题:1945. 奶牛棒球 - AcWing题库

package 蓝桥算法训练__普及组.Day8;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author snippet
 * @data 2023-02-12
 * 1945. 奶牛棒球 - AcWing题库
 */
// 双指针
public class T4 {
    static int n,ans;
    static int[] a = new int[1010];
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            a[i] = sc.nextInt();
        }
        Arrays.sort(a, 1, n+1);
        // 需要满足l1<l2 && l2<=2*l1
        for (int i = 1; i <= n-2; i++) {
            for (int j = i+1; j <= n-1; j++) {
                // 表示前两头牛的距离
                int l1 = a[j] - a[i];
                // 左指针用来指到不满足后两头牛距离>前两头牛距离的极点
                // 也就是后两头牛距离>前两头牛距离的起点
                int left = j+1;
                // 右指针指向满足后两头牛的距离<=前两头牛距离的两倍
                // 也就是后两头牛的距离<=前两头牛距离的两倍的终点
                int right = j+1;
                while (left <= n && a[left] - a[j] < l1) left++;
                while (right <= n && a[right] - a[j] <= 2 * l1) right++;
                // [左指针起点,右指针终点)
                ans += right - left;
                //System.out.println(left+"+"+right);
            }
        }
        System.out.println(ans);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值