一、前言
4.18 藍橋杯省賽即將來臨,為了更好的掌握Java基礎知識的運用,在此做一下總結,若有不足之處,望大家指正批評,
真題地址

二、知識總結
1.數值總結
基本介紹與使用
掌握資料型別的特點以及精度的使用控制,
關于精度問題(采用BigDecimal設定保留幾位小數,以及規則)的簡單總結,如下
package lq.base.structrure.base;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Random;
/**
* @AUTHOR LYF
* @DATE 2021/4/3
* @VERSION 1.0
* @DESC
*/
public class Decimal {
public static void main(String[]args){
// BigDecimal進行設定
Float f1 = 9.334F,f2 = 9.335F;
BigDecimal bigDecimal1 = new BigDecimal(f1);
BigDecimal bigDecimal2 = new BigDecimal(f2);
float f11 = bigDecimal1.setScale(2,BigDecimal.ROUND_HALF_DOWN).floatValue();// 四舍無入
float f21 = bigDecimal2.setScale(2,BigDecimal.ROUND_HALF_DOWN).floatValue();// 四舍無入
System.out.println(f11+";"+f21);
// ceil往上取,floor直接舍去
float f12 = bigDecimal1.setScale(2,BigDecimal.ROUND_CEILING).floatValue();//
float f13 = bigDecimal1.setScale(2,BigDecimal.ROUND_FLOOR).floatValue();//
System.out.println(f12+";"+f13);
// 流中的Statics,獲取平均值,最大值,計數等
IntSummaryStatistics iss = new IntSummaryStatistics();
List<Integer> list = new ArrayList<>();
for(int i=0;i<10;i++){
Random random = new
Random();
list.add(random.nextInt(20));
}
iss = list.stream().mapToInt(Integer::intValue).summaryStatistics();// summary...總結為統計
System.out.println(iss.getAverage()+";"+iss.getSum());
}
}
2.日期基本使用總結
掌握Date,SimpleFormat格式化,Calendar的基本使用,明白怎么進行與字串的相互轉換,以及使用Calendar進行日期加減操作,
/**
* @AUTHOR LYF
* @DATE 2021/4/5
* @VERSION 1.0
* @DESC
* 一、日期的基本使用
* 1.Date
* 2.Calender
* 3.format(SimpleDateFormat、DateFormat、
*
*
*/
public class DateDemo {
void test(){
// 日期類,獲取當前時間
Date date = new Date();
System.out.println("date:"+date+";time:"+date.getTime());// Mon Apr 05 18:39:21 CST 2021 ;time:1617619161143
// getTime()或1970至此 的毫秒數
System.out.println("Year:"+date.getYear()+";month:"+date.getMonth()+";day"+date.getDay()+";date"+date.getDate());
// Year:121;month:3;day1;date5 ,date才是日期?day 是代表星期幾
// Year從1900開始算的
date.setTime(2000);
System.out.println("2000ms之后:"+date); //Thu Jan 01 08:00:02 CST 1970
// 日期格式化 (按格式進行String和Date的物件相互轉換)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//按此進行格式化 MM 代表月,mm代表分
// sdf.物件方法format
sdf.format(date);//回傳String
String str = sdf.format(date);
System.out.println("格式化后:"+date.toString()+";"+str);
// String => date
try {
Date tempD = sdf.parse(str);
System.out.println("tempD:"+tempD+";"+sdf.format(tempD));
}catch (Exception e){
e.printStackTrace();
}
// Calender 更為完善復雜,方便進行日期的增減
Calendar calendar = Calendar.getInstance();// 獲取當前時期
System.out.println(calendar);
// 創建指定日期
calendar.set(2009,6-1,9);
calendar.add(Calendar.DATE,1);
System.out.println(calendar.toString()+";"+calendar.getTime());
// 通過 calender 的 getTime 與Date建立聯系,Date又可以進行SimpleDateFormat格式化以及字串相互轉換
}
public static void main(String[]args){
DateDemo dateDemo = new DateDemo();
dateDemo.test();
}
}
3.字串操作
掌握string的基本方法以及與數值型別相互轉換的靈活使用(比如進行判斷數的具有不含某些數字然后進行遍歷計數),掌握Regex的基本使用,方便進行查找匹配,比如說BobAlice那題就可使用該方法進行簡單化,
-
- 轉義字符 \n,\等,但在Java中需要\才能表示,\(就相當于( 進行匹配( \d匹配數字
-
-
- 與 + 匹配0個或多個 1個與多個
-
-
- . 匹配一個
-
- ?匹配一個
-
- {n,m} 匹配n到m次數
- 6.[abc] 包含字符集中的某個
-
- ^非
- 基本字符: . ? [a-z] [^a-z] \( 轉義字符
- 進行擴展: {n,m} * +
- 參考:https://www.runoob.com/java/java-regular-expressions.html
// 人物相關性分析
void test8(){
Scanner scanner = new Scanner(System.in);
String str2 =scanner.nextLine();
int k = Integer.valueOf(str2);
String str = scanner.nextLine();
// 法1直接使用find方法記錄位置
// regex
String regexAlice = "Alice";
String regexBob = "Bob";
Pattern pattern = Pattern.compile(regexAlice);
Pattern pattern1 = Pattern.compile(regexBob);
Matcher matcher = pattern.matcher(str);
Matcher matcher1 = pattern1.matcher(str);
List<Integer> listAlice = new ArrayList<>();
List<Integer> listBob = new ArrayList<>();
while(matcher.find()){
listAlice.add(matcher.start()); //開始index
}
while(matcher1.find()){
listBob.add(matcher1.start()); //開始index
// System.out.print(matcher1.group()+"->");
}
int count=0;
for(int i =0;i<listAlice.size();i++){
// 尋找鄰近是否有Bob
int index= listAlice.get(i);
for(int j = 0;j<listBob.size();j++){
if(Math.abs(listBob.get(j)-index)<=k){
count++;
}
}
}
System.out.println(count);
}
其他數學問題GCD和LCM
// 轉輾相除
// 最大公約數
int gcd(int a,int b){
if(a%b==0)
return b;
else
return gcd(b,a%b);
}
// 最小公倍數
int lcm(int a,int b){
return a*b/gcd(a,b);
}
//素數,,逆置也可采用類似的模板
boolean getPrimer(int n){
for(int i =2;i*i<=n;i++){
if(n%i==0)
return false;
}
4.集合的簡單使用
5.簡單的演算法
全排列
// 遞回辦法
void permutation(int[]nums,int start){
// 前面已經排好序,直接輸出
if(start==nums.length-1){
System.out.println(Arrays.toString(nums));
}
for(int i =start;i<nums.length;i++){
swap(nums,start,i);// 將第一個與后面的依次交換
permutation(nums,start+1);// 排列下一個
swap(nums,start,i);// 交換回來
}
}
void swap(int[] nums,int i,int j ){
int temp = nums[i];
nums[i]=nums[j];
nums[j]=temp;
}
// 字典排序
public static void main(String[]args){
FullPermutation fp = new FullPermutation();
int[] nums={1,2,3,4};
fp.permutation(nums,0);
}
BFS
package lq.base.structrure.graph;
import java.io.*;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Stack;
/**
* @AUTHOR LYF
* @DATE 2021/4/9
* @VERSION 1.0
* @DESC
* 1.問題
* 注意X,Y和 row、col的對應關系
* 2.
*/
// 點資訊
class Point{
int x,y;
Point pro;// 前驅
public Point(int x, int y, Point pro) {
this.x = x;
this.y = y;
this.pro = pro;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Point getPro() {
return pro;
}
public void setPro(Point pro) {
this.pro = pro;
}
@Override
public String toString() {
return "Point{" +
"x=" + x +
", y=" + y +
'}';
}
}
public class BFS2 {
//int n;
static int[][] maze = new int[100][100];//new int[][]
int tX[]={1,0,-1,0};
int tY[]={0,-1,0,1};// 使用向量進行簡化方向判斷使用
String[] steep={"R","U","L","D"};
static {
//FileInputStream fis = new FileInputStream("")
try {
InputStream ips = new FileInputStream("E:\\IdeaProjects\\java-base\\src\\lq\\base\\structrure\\graph\\maze.txt");
InputStreamReader isr= new InputStreamReader(ips);
BufferedReader br = new BufferedReader(isr);
String temp = null;
int row=0,col=0;
while((temp=br.readLine())!=null){
col= temp.length();
for(int i = 0;i<col;i++){
maze[row][i]=Integer.valueOf(temp.substring(i,i+1));
}
row++;
}
System.out.println("棋盤:");
for (int i = 0;i<row;i++){
for(int j = 0;j<col;j++){
System.out.print(maze[i][j]+" ");
}
System.out.println();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
void bfs(){
Queue<Point> queue = new ArrayDeque<>();// 也可以使用LinkedList
queue.add(new Point(0,0,null));
while (!queue.isEmpty()){
Point cur = queue.poll();
maze[cur.getY()][cur.getX()]=1;//訪問
if(cur.getX()==5&&cur.getY()==3){// 訪問終點
Stack<Point> stack = new Stack<>();
while (cur.getPro()!=null){
stack.push(cur);
cur = cur.getPro();
}
System.out.println("可以訪問到,且最近路徑長度為:"+stack.size()+";軌跡為:");
// 需要進行反向,也可以考慮遞回回退列印
String direction = "";
stack.push(new Point(0,0,null));//加入起點
while (!stack.isEmpty()){
Point cur2 = stack.peek();
stack.pop();
System.out.print(cur2+"->");
if(stack.isEmpty()){
break;
}else{
Point next = stack.peek();//不要洗掉,因下次是起點
for(int i = 0;i<4;i++){
if(cur2.getX()+tX[i]==next.getX()&&cur2.getY()+tY[i]== next.getY()){
direction=direction+steep[i];
break;
}
}
}
}
System.out.println();
System.out.println("轉向情況為:"+direction);
}
for(int i =0;i<4;i++){
int x = cur.getX()+tX[i];
int y = cur.getY()+tY[i];
if(x>5||x<0||y<0||y>3)//排除邊界
continue;
if(maze[y][x]==0)// x 列 y 行 注意對應關系
queue.add(new Point(x,y,cur));
}
}
}
public static void main(String[]args){
BFS2 bfs2 = new BFS2();
bfs2.bfs();
}
}
n皇后
static int n=3;
// 棋盤
static boolean [][] plain = new boolean[n][n];
static {
for(int i =0 ;i<n;i++){
for(int j = 0 ;j<n;j++){
plain[i][j]=false;
}
}
}
// 若只判斷行,則是排列數排列
boolean judge(int row,int col){//
// 判斷列
for(int i =0;i<row;i++){
if(plain[i][col]==true){
return false;
}
}
// 判斷斜方向
return true;
}
void queen(int i){//,int j 下在第i+1行
if(i>=n){
for(int k=0;k<n;k++){
for(int l=0;l<n;l++){
//System.out.print(plain[k][l]+" ");
if(plain[k][l]) //輸出選擇的數,
System.out.print(l);
}
// System.out.println();
}
System.out.println();
}else{
for(int k=0;k<n;k++){
plain[i][k]= true;
if(judge(i,k)){
queen(i+1);
}
plain[i][k]=false;//回退!!
}
}
}
public static void main(String[]args){
EightQueen eightQueen = new EightQueen();
eightQueen.queen(0);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/274391.html
標籤:java
下一篇:實作ApplicationContextAware介面后呼叫時候報空指標NullPointerException例外
