一、前言
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);
}