笔试狂刷--Day9(模拟 + dp + 规律)

大家好,我是LvZi ,今天带来笔试狂刷--Day9

一.添加逗号

题目链接:添加逗号

分析:

模拟

代码:

java 复制代码
import java.util.*;


// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        char[] tmp = String.valueOf(n).toCharArray();
        char[] s = new char[tmp.length];

        for (int i = 0; i < tmp.length; i++) {
            s[i] = tmp[tmp.length - 1 - i];
        }

        StringBuffer sb = new StringBuffer();
        int j = 0;

        while (j < s.length) {
            if ((j != 0) && ((j % 3) == 0)) {
                sb.append(",");
            }

            sb.append(s[j]);
            j++;
        }

        System.out.println(sb.reverse().toString());
    }
}

二.跳台阶

题目链接:跳台阶

分析:

基础的动态规划

代码:

java 复制代码
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();

        if (n == 1) {
            System.out.println(1);

            return;
        }

        if (n == 2) {
            System.out.println(2);

            return;
        }

        int a = 1;
        int b = 2;
        int c = 0;

        for (int i = 3; i <= n; i++) {
            c = a + b;
            a = b;
            b = c;
        }

        System.out.println(c);
    }
}

三.

题目链接:扑克牌顺子

分析:

找规律

如果能构成顺子,则所有非零元素一定满足

  • 不存在重复元素
  • max - min <= 4

代码:

java 复制代码
    public boolean IsContinuous (int[] numbers) {
        // write code here
        boolean[] hash = new boolean[14];

        int max = 0, min = 14;
        for(int x : numbers) {
            if(x != 0) {
                if(hash[x]) return false;// 同一数字出现两次  一定不是顺子
                hash[x] = true;
                max = Math.max(max,x);
                min = Math.min(min,x);
            }
        }

        return max - min <= 4;
    }
相关推荐
人间乄惊鸿客7 小时前
c++ 右值
c++·算法
fanstuck8 小时前
AI 客服答错了,三分钟分清是 Prompt 还是系统的锅
算法
田里的水稻9 小时前
FA_编程算法200个分类列数-SUM
算法·分类·数据挖掘
猿人谷11 小时前
从百万个 Pareto 解中选出 100 个:Riesz s-Energy 如何实现高效的多样性选择?
后端·算法·aigc
地平线开发者11 小时前
地平线 × 同济无人车队:PointPillars 从浮点模型到 BPU 部署
算法·自动驾驶
iiiiii1112 小时前
从最大熵到 SAC:Soft Q-Learning 与 Soft Actor-Critic 公式推导笔记
人工智能·笔记·算法·机器学习·强化学习
田里的水稻13 小时前
FA_排序算法-冒泡排序
java·算法·排序算法
AAA修煤气灶刘哥15 小时前
AI 写的 SQL 跑通了,为什么和财务报表对不上?
人工智能·后端·算法