【牛客刷题15】参数解析与跳石板

题目一

1.题目:参数解析

题目链接:参数解析

在这里插入图片描述

2.思路

  1. 第一次遍历字符串,记录参数的个数,需要注意的是空格数量比参数数量少1,并且双引号中的空格数量是不能记录的。
  2. 第二次遍历字符串是定义一个flag = 1,并且遇到双引号就进行异或操作,从而判断此时的字符在双引号里面还是外面。

3.代码实现

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        int count = 0;
        for(int i =0;i<str.length();i++){
            if(str.charAt(i) == '"'){
                i++;
                while(str.charAt(i) != '"' ){
                    i++;
                }
            }
            if(str.charAt(i) == ' '){
                count++;
            }
        }
        System.out.println(count+1);
        //输出字符
        int flag = 1;
        for(int i =0;i<str.length();i++){
            if(str.charAt(i) == '"'){
                flag ^=1;
            }
            if(str.charAt(i) != ' ' && str.charAt(i) != '"'){
                System.out.print(str.charAt(i));
            }
            if(str.charAt(i) == ' ' && flag == 0){
                System.out.print(str.charAt(i));
            }
            if(str.charAt(i) == ' ' && flag == 1){
                System.out.println();
            }
        }
        
    }
}

题目二

1.题目:跳石板

题目链接:跳石板

在这里插入图片描述

2.思路

  这一道题采用的动态规划的思想来做,那么动态规划的题,最重要的一步是找到其内在联系的公式。这里先不言本题的公式是什么,先让我们按照题目分析这道题。
在这里插入图片描述

3.代码实现

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] stem = new int[m+1];//为了是数组的编号跟石板的下标对应在一块
        //初始化数组
        for(int i=0; i<m+1;i++){
            stem[i] = Integer.MAX_VALUE;//放置一个最大的不可能到达的数字
        }
        stem[n] = 0;//初始化第一块石板为0
        //开始跳跃
        for(int i =0;i<m;i++){
            //如果stem[i]里的数字为初始化哪个的化,跳出循环
            if(stem[i] == Integer.MAX_VALUE){
                continue;
            }
            List<Integer> list = div(i);
            //j表示一次可以跳多少个石板——也就是约数
            //i表示当前石板的编号
            for(int j : list){
                if(i+j<=m && stem[i+j] != Integer.MAX_VALUE){
                    stem[i+j] = Math.min(stem[i+j],stem[i]+1);
                }else if(i+j <= m){
                    stem[i+j] = stem[i]+1;
                }
            }
        }
        if(stem[m] == Integer.MAX_VALUE){
            System.out.println(-1);
        }else{
            System.out.println(stem[m]);
        }
    }
    //找出所有约数
    public static List<Integer> div(int num){
        List<Integer> list = new ArrayList<Integer>();
        for(int i = 2;i*i<=num; i++){
            if(num % i ==0){
                list.add(i);
                if(num/i != i ){
                    list.add(num/i);
                }
            }
        }
        return list;
    }
}

4.反思

  由于对动态规划这一块不太熟,因此搞懂这道题还是花费了不少时间,其中肯定会有一些讲得不清楚的地方。因此这里放两篇讲解的比较好的博客:

(1)【动态规划】跳石板(逐步分析,详例)
(2)动态规划之跳石板

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

十叶知秋

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

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

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

打赏作者

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

抵扣说明:

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

余额充值