当前位置:网站首页>经典题目刷一刷
经典题目刷一刷
2022-04-21 23:14:00 【红苹果超好吃】
目录
1.二进制的插入
①题目及示例:
②方法解析:
按照题目意思,待插入的那一段均为0,那么我们很容易就会想到用或运算来完成,而从哪里插入就将待插入值右移多少位即可。注意!二进制的位数从0开始由低到高。我们画一个图来进一步解释:
③代码如下:
import java.util.*; public class BinInsert { public int binInsert(int n, int m, int j, int i) { // write code here m<<=j; return m|n; } }
2.查找组成一个偶数的最近两个素数
①题目及示例:
②方法解析:
由题,我们能够分析到,本题要找的东西需要满足几个条件,一是组成偶数的数需要是两个素数,二是需要这对素数差值最小。
其中需要注意因为要使差值最小,且数据都是成对出现,那么最小的差值,必定是离中间最近的地方,所以我们直接从中间来开始搜寻判断是不是素数。
③代码如下:
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(); int mid=n/2; for(int i=mid;i>0;i--){ if(isPrime(i)&&isPrime(n-i)){ System.out.println(i); System.out.println(n-i); break; } } } }//写一个函数判断它是不是素数 public static boolean isPrime(int n){ for(int i=2;i<=Math.sqrt(n);i++){ if(n%i==0){ return false; } } return true; } }
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)=='"'){ do{ i++; }while(str.charAt(i)!='"'); } if(str.charAt(i)==' '){ count++; } } System.out.println(count+1); //因为在引号里的空格是要打出来的,所以我们设置一个标志位flg,当其要打印引号里的值时特殊处理 int flg=1; for(int i=0;i<str.length();i++){ //遇到第一个双引号,flag变为0, //遇到第二个双引号结束后flag重新变为1 //只要在打印双引号中的内容的时候flag的值始终为0 if(str.charAt(i)=='"'){ flg^=1; } //除了双引号和特殊空格以外的字符都要打印 if(str.charAt(i)!=' '&&str.charAt(i)!='"'){ System.out.print(str.charAt(i)); } //双引号中的空格需要打印 if(str.charAt(i)==' '&&flg==0){ System.out.print(str.charAt(i)); } //双引号外碰到空格,需要换行 if(str.charAt(i)==' '&&flg==1){ System.out.println(); } } } }
4.跳石板
①题目及示例:
②方法解析:根据题意,我们可以默认把每个值的数据都设置为一个默认值,每跳跃一次,在原来的基础上进行+1(将1 - M个石板看做一个结果数组stepNum,每个stepNum[i]储存着从起点到这一步最小的步数,其中0为不能到达。 从起点开始对stepNum进行遍历,先求i的所有约数(即stepNum[i]能走的步数),然后更新那 几个能到达的位置的最小步数。如果不能到达则更新为此时位置的最小步数 + 1,如果是能到达的就更新为min(已记录的最小步数,此处的最小步数 + 1)),遍历一遍后得到结果。 )
a.设置一个数组来供我们记录(记录的值为跳跃对应的次数)
b.求一个数的约数
c.利用动态规划找出最小跳的次数
(我们来用一个图来进行具体的分析)
③代码如下:
import java.util.*; public class Main{ public static void main(String[] args) { Scanner sca = new Scanner(System.in); int n = sca.nextInt(); int m = sca.nextInt(); //和数组下标对应这里为m+1 int[] step = new int[m+1]; for(int i =0;i < m+1;i++){ //设定一个初始值 step[i] =Integer.MAX_VALUE; }//起始位置n为0步 step[n] = 0; for(int i =n;i < m;i++) { //当设计2到初始值,说明并没有跳,直接进行下次循环 if(step[i] == Integer.MAX_VALUE) { continue; } //求当前值的约数 List<Integer> list = div(i); //j代表一次可以跳几块石板,此时j是list中的约数,本质上就是记录着跳几步 //i代表当前石板的编号次数 for(int j : list){ if(i+j<=m && step[i+j] !=Integer.MAX_VALUE) {//表明在这之前已经有跳到这里的次数了 //动态规划的体现(已经有到这里的步数和下一步即将到这里的步数取最小值) step[i+j] = Math.min(step[i+j],step[i]+1); }else if(i+j<=m) {//表明在这之前还没有跳到这里的次数,那么就记录当前次数 step[i+j] = step[i]+1; } } }//跳出循环后i+j=m; if(step[m]==Integer.MAX_VALUE) {//最终m处的次数若是为默认值,那么说明没有跳到这里的,失败,返回-1 System.out.println(-1); }else{ System.out.println(step[m]); } } //求约数 public static List<Integer> div(int num) { List<Integer> list = new ArrayList<>(); for(int i =2;i*i<=num;i++){ if(num%i==0) { list.add(i); if(num/i!=i){//确保list中的数不重复 list.add(i); } } } return list; } }
版权声明
本文为[红苹果超好吃]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_58850105/article/details/124316295
边栏推荐
- Why is everything console I can't come out. console. log(depsMap)
- 自定义模板问题求助,自动添加时间日期
- Some cold thoughts behind the popularity of microservices, middle office, RPA and low code
- 瑞芯微芯片AI部分开发记录 第一节 《PC端环境搭建1》
- Pytoch framework | torch nn. modules. Module(nn.Module)
- Ros2 robot modeling URDF 8.2rviz2 visual mobile robot model
- Single step analysis of double linked list insertion and deletion (14)
- 新独立版抖音口红机全修复版本附视频教程
- Kubernetes---Secret配置管理
- 音视频基本概念和FFmpeg的简单入门
猜你喜欢

8.4 control of wheel motion of mobile robot in rodf robot modeling

Some cold thoughts behind the popularity of microservices, middle office, RPA and low code

Day code 300 lines learning notes day 46
![[basic learning of FPGA ------ ov7725 camera module]](/img/74/71ead5bca4619456841ce3a4c95b65.png)
[basic learning of FPGA ------ ov7725 camera module]

5. QT using MySQL

2022電視盒子加購榜排名,超一多半的用戶選購當貝盒子

大厂面试必备技能,android音视频框架

大厂面经合集,这些知识点你会吗

音视频基本概念和FFmpeg的简单入门

Teach you to easily solve CSRF Cross Site Request Forgery Attack
随机推荐
Finally, someone made it clear that this is the global one-piece network technology with low delay
6. Example of QT using MySQL
大厂面经合集,这些知识点你会吗
Kubenetes (3) -- network communication (2) -- flannel and calico
Selection and evolution of microservices under cloud native architecture
[basic learning of FPGA ------ ov7725 camera module]
MySQL problem solving_ Multi table joint query_ Please take out the corresponding data and output it in ascending order of accuracy.
1141: C language training - a hundred dollars and a hundred chickens_ Pruning cycle
Basic concepts of audio and video and a simple introduction to ffmpeg
MySQL problem solving_ Multi table joint query_ Take out the average probability that the user will brush the question again after one day
技术、产品、品牌都不是问题,对上汽奥迪而言,这2点或生死攸关
2022電視盒子加購榜排名,超一多半的用戶選購當貝盒子
NN in pytoch Brief introduction to adaptive avgpool2d (output_size)
Track and trigger
What is the core design of Dadi's performance advantage in the cache hit scenario?
Wiki. JS configure LDAP authentication
April 22, 2022 Daily: a new face attribute editing framework based on transformer
Golang kicks leetcode game 289
Swoole high performance in memory database use and configuration tutorial
点滴浓缩洁净,洗衣液行业的破局之路
https://www.nowcoder.com/practice/30c1674ad5694b3f8f0bc2de6f005490?tpId=8&&tqId=11019&rp=1&ru=/activity/oj&qru=/ta/cracking-the-coding-interview/question-ranking
②方法解析:
②方法解析:

