当前位置:网站首页>30 day question brushing plan (IV)
30 day question brushing plan (IV)
2022-07-28 13:48:00 【Red apples are delicious】

insist !!!
Catalog
2. Legal bracket sequence judgment
4. Find the least common multiple
1.Fibonacci The sequence
① Topics and examples :
② Method resolution :
The meaning of this question is to give us a number , Let's find the minimum value that its minimum number of steps becomes Fibonacci number . And we know that it is nothing more than one of the numbers greater than it or less than it .
We don't use recursive method here , Instead, we use the relationship between three adjacent numbers , When n After jumping out of the loop , It means that at this time f2 It just surpassed n Fibonacci number of itself , And then f1 It is f2 The previous Fibonacci number of , therefore , We can know at this time f1<N<f2. So at this time, we just need to compare the difference , Select a number with a small difference . The code is as follows :
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. Legal bracket sequence judgment
① Topics and examples :
② Method resolution :
Judge whether the brackets are legal . Consider the following . First of all , Must be all parentheses , Letters and other non parentheses cannot appear . And if it's legal , Then the left and right parentheses must appear in pairs , Then the number must be equal . We use two ways to find .
a. Solve directly :
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. Ask through the stack : Put the left bracket on the stack , When you encounter a close bracket , Determine whether the stack is empty , Not empty stack , Then one , Until the stack is empty . Finally, you only need to judge whether the next stack is empty , Empty indicates that it is over ,return true; conversely .
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++){ // when '(' Stack when if(A.charAt(i)=='('){ stack.push(A.charAt(i)); // when ')' Determine whether the stack is empty , If the stack is empty , It indicates that the quantity does not match , That is, it can't match // If the stack is not empty , Determine whether the top element of the stack is '(' }else if(A.charAt(i)==')'){ if(stack.isEmpty()){ return false; }else //if(stack.peek()=='('), Don't judge this sentence , Because the original stack is the left parenthesis , As long as the stack is not empty , Then the top element of the stack must be '(' { stack.pop(); } // It means that the input is neither the left parenthesis , It's not a right parenthesis }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. Two sort methods
① Topics and examples :
② Method resolution :
This question mainly examines the comparison of strings , One is to pass. compareTo To compare dictionary forms , The second is to compare by the length of the string . It should be noted that the input string is put into the same string array , Then compare the values according to the subscript value . At the same time, explain the meaning of several methods in the code .
(1)
BufferedReaderIt is a packaging class designed to provide reading efficiency , It can wrap character streams . Text can be read from the character input stream , Buffer individual characters , So as to realize the character 、 Efficient reading of arrays and rows . This is read directly when inputting byte stream .(2)readLine() Method , Pay attention to throwing exceptions when using
(3)Integer.parseInt () Method : Put the string s Parse into signed int Basic types .
meanwhile ,Integer.valueOf(s): Put the string s It can be interpreted as Integer object type .
Here is a packing process .
You can refer to the blogger's blog :(3 Bar message ) Integer.parseInt(s) And Integer.valueOf(s) The difference between _HD243608836 The blog of -CSDN Blog
The code is as follows :
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. Find the least common multiple
① Topics and examples :
② Method resolution :
We need to know first , If the least common multiple is required , You can use the product of these two numbers / The greatest common divisor of these two numbers .
The greatest common divisor of two numbers can be achieved by rolling and dividing . The core of the rolling division method is to divide the divisor by the remainder , Until the remainder is 0 The divisor of time is the greatest common divisor . therefore , The code is as follows :
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;// The greatest common factor // The least common multiple is required , Just divide your grades by the greatest common divisor // The greatest common divisor can be obtained by rolling and dividing if(a<b){ int tmp=a; a=b; b=tmp; } while(a%b!=0){// Use the rolling division method to find the greatest common factor int tmp=b; b=a%b; a=tmp; } System.out.println(times/b); } }besides , We can find the greatest common factor recursively , The code is as follows :
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); } }It's a little difficult here , You need to pay attention .
边栏推荐
- [Architecture] reading notes of three micro service books with high scores
- DDoS protection with iptables
- No swagger, what do I use?
- 产品经理:岗位职责表
- Realize the mutual value transfer between main window and sub window in WPF
- How to check if the interface cannot be adjusted? I didn't expect that the old bird of the 10-year test was planted on this interview question
- POJ3259虫洞题解
- P1797重型运输 题解
- DOJNOIP201708奶酪题解
- 倒计时 2 天!2022 中国算力大会:移动云邀您共见算力网络,创新发展
猜你喜欢

不用Swagger,那我用啥?

Use non recursive method to realize layer traversal, preorder traversal, middle order traversal and post order traversal in binary tree

Some thoughts on.Net desktop development

《如何打一场数据挖掘赛事》入门版
![[dark horse morning post] byte valuation has shrunk to $270billion;](/img/58/8d5c78d919ed60bc833ec4daa22e23.jpg)
[dark horse morning post] byte valuation has shrunk to $270billion; "Second uncle" video author responded to plagiarism; Renzeping said that the abolition of the pre-sale system of commercial housing

微信小程序中自定义模板

Org.apache.ibatis.exceptions.toomanyresultsexception

比XShell更好用、更现代的终端工具!

Denial of service DDoS Attacks

Map tiles: detailed explanation of vector tiles and grid tiles
随机推荐
R language test sample proportion: use prop The test function performs the single sample proportion test to calculate the confidence interval of the p value of the successful sample proportion in the
Children's programming electronic society graphical programming level examination scratch Level 2 real problem analysis (judgment question) June 2022
Kotlin learning notes 3 - lambda programming
POJ3275 Ranking the Cows题解
R语言使用lm函数构建多元回归模型(Multiple Linear Regression)、并根据模型系数写出回归方程、使用confint函数给出回归系数的95%置信区间
使用 IPtables 进行 DDoS 保护
I miss the year of "losing" Li Ziqi
R语言可视化散点图、使用ggrepel包的geom_text_repel函数避免数据点之间的标签互相重叠(使用参数xlim和ylim将标签添加到可视化图像的特定区域、指定标签线段并添加箭头)
Beyond Istio OSS——Istio服务网格的现状与未来
C语言:随机生成数+归并排序
DOJP1520星门跳跃题解
C language: random generated number + merge sort
Half wave rectification light LED
Beyond istio OSS -- current situation and future of istio Service Grid
DDoS protection with iptables
Remember to use pdfbox once to parse PDF and obtain the key data of PDF
R language ggplot2 visualization: visualize the scatter diagram and add text labels to the data points in the scatter diagram, using geom of ggrep package_ text_ The rep function avoids overlapping da
After finishing, help autumn move, I wish you call it an offer harvester
【黑马早报】字节估值缩水,降至2700亿美元;“二舅”视频作者回应抄袭;任泽平称取消商品房预售制是大势所趋;美联储宣布再加息75个基点...
.net for subtraction, intersection and union of complex type sets
https://www.nowcoder.com/practice/18ecd0ecf5ef4fe9ba3f17f8d00d2d66?tpId=85&&tqId=29846&rp=1&ru=/activity/oj&qru=/ta/2017test/question-ranking




