当前位置:网站首页>30天刷题计划(四)
30天刷题计划(四)
2022-07-28 12:38:00 【红苹果超好吃】

坚持!!!
目录
1.Fibonacci数列
①题目及示例:
②方法解析:
这个题的意思就是说给我们一个数,我们来找到它最小步数变为斐波那契数的这个最小值。而我们知道无非是大于其或者小于其的数中的一个。
我们这里不采用递归的方法,而是采用相邻三个数间存在的关系,当n跳出循环后,就说明此时f2刚好是刚刚超过了n本身的斐波那契数,而此时f1正是f2的前一个斐波那契数,因此,我们就可以知道此时f1<N<f2.所以这个时候我们只需要比较差值,选取差值较小的数即可。代码如下:
import java.util.*; public class Main { public static void main(String[]args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int f1=0; int f2=1; while(n>f2){ int f3=f1+f2; f1=f2; f2=f3; } int min=Math.min(n-f1,f2-n); System.out.println(min); } }
2.合法括号序列判断
①题目及示例:
②方法解析:
判断括号是否合法。要考虑以下几点。第一,必须全是括号,不能出现字母等其他非括号。且如果合法,那么必然左右括号成对出现,则数目必然相等。我们采用两种方式来求。
a.直接求解:
import java.util.*; public class Parenthesis { public boolean chkParenthesis(String A, int n) { // write code here int count1=0; int count2=0; for(int i=0;i<A.length();i++){ if(A.charAt(i)!='('&&A.charAt(i)!=')'){ return false; } if(A.charAt(i)=='('){ count1++; } if(A.charAt(i)==')'){ count2++; } } if(count1==count2){ return true; } return false; } }b.通过栈来求:将左括号入栈,当遇到右括号时,判断一下是否为空栈,不为空栈,则出一个,一直到栈为空为止。最后只需要判断下栈是否为空,为空表示已经出完了,return true;反之。
import java.util.*; public class Main { public static boolean chkParenthesis(String A, int n) { // write code here if(n%2!=0){ return false; } Stack<Character>stack=new Stack<>(); for(int i=0;i<n;i++){ //当是'('时就入栈 if(A.charAt(i)=='('){ stack.push(A.charAt(i)); //当是')'时就判断栈是否为空,若是栈为空,则表明数量不匹配,即匹配不了 // 栈不为空的话,判断栈顶元素是否是'(' }else if(A.charAt(i)==')'){ if(stack.isEmpty()){ return false; }else //if(stack.peek()=='('),不用判断这一句,因为本来入栈的都是左括号,只要栈不为空,那么栈顶元素必然为'(' { stack.pop(); } //则表示输入的既不是左括号,也不是右括号 }else{ return false; } } return stack.isEmpty(); } public static void main(String[] args) { Scanner sc=new Scanner(System.in); String A=sc.nextLine(); int n=sc.nextInt(); System.out.println(chkParenthesis(A, n)); } }
3.两种排序方法
①题目及示例:
②方法解析:
本题主要考查字符串的比较,一是通过compareTo来进行字典形式的比较,二是通过字符串的长度来比较。需要注意的是将输入的字符串放入的是同一个字符串数组,然后根据下标值来对各个值进行比较。同时说明一下代码中的几个方法的含义。
(1)
BufferedReader是为了提供读的效率而设计的一个包装类,它可以包装字符流。可以从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。这里直接是在输入字节流的时候读取的。(2)readLine()方法,注意使用的时候要抛出异常
(3)Integer.parseInt ()方法:把字符串s解析成有符号的int基本类型。
同时,Integer.valueOf(s):把字符串s解析成Integer对象类型。
这里是一个装箱过程。
可以参考一下这位博主的博客:(3条消息) Integer.parseInt(s)与Integer.valueOf(s)的区别_HD243608836的博客-CSDN博客
代码如下:
import java.util.*; import java.io.*; public class Main { public static boolean lexicographically(String[] str){ for(int i=0;i<str.length-1;i++){ if(str[i].compareTo(str[i+1])>0){ return false; } }return true; } public static boolean lengths(String []str){ for(int i=0;i<str.length-1;i++){ if(str[i].length()>str[i+1].length()){ return false; } }return true; } public static void main(String[]args)throws IOException{ BufferedReader re=new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(re.readLine()); String[] str=new String[n]; for(int i=0;i<n;i++){ str[i]=re.readLine(); } if(lengths(str)&&(lexicographically(str))){ System.out.println("both"); }else if(lengths(str)){ System.out.println("lengths"); }else if(lexicographically(str)){ System.out.println("lexicographically"); }else{ System.out.println("none"); } } }
4.求最小公倍数
①题目及示例:
②方法解析:
我们首先需要知道,要求最小公倍数的话,可以用这两个数的乘积/这两个数的最大公约数。
而两个数的最大公约数可以通过辗转相除法来实现。辗转相除法的核心是用除数除以余数,直到余数为0时的除数就是最大公约数。所以,代码如下:
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int a=sc.nextInt(); int b=sc.nextInt(); int times=a*b; int max=0;//最大公因数 //要求最小公倍数,就用成绩除以最大公约数 //最大公约数又可以用辗转相除法来求得 if(a<b){ int tmp=a; a=b; b=tmp; } while(a%b!=0){//利用辗转相除法来求最大公因数 int tmp=b; b=a%b; a=tmp; } System.out.println(times/b); } }除此之外,我们可以通过递归的方式求最大公因数,代码如下:
import java.util.*; class Main { public static int digui(int x,int y){ if(y==0){ return x; }else { return digui(y,x%y); } } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int a=sc.nextInt(); int b=sc.nextInt(); int max=digui(a,b); System.out.println(max); } }这里有点难饶,需要好好注意一下。
边栏推荐
- 比XShell更好用、更现代的终端工具!
- C language: quick sorting of sequential storage structure
- Strict mode -- let and const -- arrow function -- Deconstruction assignment -- string template symbol -- set and map -- generator function
- 《如何打一场数据挖掘赛事》入门版
- 长封闭期私募产品再现 业内人士看法各异
- 数据库系统原理与应用教程(060)—— MySQL 练习题:操作题 11-20(四)
- Org.apache.ibatis.exceptions.toomanyresultsexception
- Why is crypto game changing the game industry?
- What if the server cannot be connected (the original server cannot find the target resource)
- 【黑马早报】字节估值缩水,降至2700亿美元;“二舅”视频作者回应抄袭;任泽平称取消商品房预售制是大势所趋;美联储宣布再加息75个基点...
猜你喜欢

今日睡眠质量记录75分

二舅能治好年轻人的精神内耗吗?

DDoS protection with iptables

Excellent performance! Oxford, Shanghai, AI Lab, Hong Kong University, Shangtang, and Tsinghua have joined forces to propose a language aware visual transformer for reference image segmentation! Open

ES6 null merge operator (?)
![[报错]使用ssh登陆到另一台机器后,发现主机名还是自己|无法访问yarn8088](/img/81/641a5b3445534fc3b8c87ee6deaa64.png)
[报错]使用ssh登陆到另一台机器后,发现主机名还是自己|无法访问yarn8088

面经整理,助力秋招,祝你称为offer收割机

国产API管理工具Eolink太好用了,打造高效的研发利器

Parent and child of treeselect

Strict mode -- let and const -- arrow function -- Deconstruction assignment -- string template symbol -- set and map -- generator function
随机推荐
我秃了!唯一索引、普通索引我该选谁?
Cesium pit -- pit used by various API calls and API itself
LyScript 获取上一条与下一条指令
What is the optimization method of transaction and database
Excellent performance! Oxford, Shanghai, AI Lab, Hong Kong University, Shangtang, and Tsinghua have joined forces to propose a language aware visual transformer for reference image segmentation! Open
Jenkins--持续集成服务器
GO语言-栈的应用-表达式求值
DOJP1520星门跳跃题解
倒计时 2 天!2022 中国算力大会:移动云邀您共见算力网络,创新发展
比XShell更好用、更现代的终端工具!
Children's programming electronic society graphical programming level examination scratch Level 2 real problem analysis (judgment question) June 2022
gicv3 spi register
力扣 2354. 优质数对的数目
FFT wave simulation
POJ3259虫洞题解
2021-10-06
paddleClas分类实践记录
SQL每日一练(牛客新题库)——第4天:高级操作符
严格模式——let和const——箭头函数——解构赋值——字符串模板symbol——Set和Map——生成器函数
微念“失去”李子柒的这一年
https://www.nowcoder.com/practice/18ecd0ecf5ef4fe9ba3f17f8d00d2d66?tpId=85&&tqId=29846&rp=1&ru=/activity/oj&qru=/ta/2017test/question-ranking




