java通关整理汇总-Java基础、计算机网络、数据库、设计模式、框架、算法模板、笔试
1.A-Z顺时针打印
输入m,n,然后顺时针把A-Z打印到数组中
package mihayou2;
import java.util.Scanner;
/**
* @author liu
* @Description
*/
public class main1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
char[][] ch = new char[m][n];
char[] a = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
char[] b = new char[n*m];
int top = 0,bottom = m-1,left = 0,right = n-1;
int k = 0;
while (true){
//从左到右
for(int i = left;i<=right;i++){
ch[top][i] = a[(k%26)];
k++;
}
if(++top >bottom) break;
//从上到下
for(int i = top;i<=bottom;i++){
ch[i][right] = a[(k%26)];
k++;
}
if(--right <left) break;
//从右到左
for(int i = right;i>=left;i--){
ch[bottom][i] = a[(k%26)];
k++;
}
if(--bottom <top) break;
//从下到上
for(int i = bottom;i>=top;i--){
ch[i][left] = a[(k%26)];
k++;
}
if(++left >right) break;
}
for(int i = 0;i<m;i++){
for(int j = 0;j<n;j++){
System.out.print(ch[i][j] + " ");
}
System.out.println();
}
}
}
2.正则匹配(. * +)
package mihayou2;
import java.util.Scanner;
/**
* @author liu
* @Description
*/
public class main2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
String p = sc.next();
System.out.println(isSubString(s,p));
}
private static boolean isSubString(String s,String p){
if(s==null||p==null){
return false;
}
int rows = s.length();
int columns = p.length();
boolean[][] dp = new boolean[rows+1][columns+1];
dp[0][0] = true;
for(int j = 1;j<columns+1;j++){
if(p.charAt(j-1) == '*' && dp[0][j-2]){
dp[0][j] = true;
}
}
for(int i = 1;i<rows+1;i++){
for(int j = 1;j<columns+1;j++){
char nows = s.charAt(i-1);
char nowp = p.charAt(j-1);
if(nows == nowp){
dp[i][j] = dp[i-1][j-1];
}else {
if (nowp == '.') {
dp[i][j] = dp[i-1][j-1];
}else if(nowp == '*'){
if(j>=2){
char nowLast = p.charAt(j-2);
if(nowLast == nows||nowLast=='.'){
dp[i][j] = dp[i-1][j]||dp[i][j-1];
}
dp[i][j] = dp[i][j]||dp[i][j-2];
}
}else if(nowp == '+'){
if(j>=2){
char nowpLast = p.charAt(j-2);
if(nowpLast == nows||nowpLast == '.'){
dp[i][j] = dp[i-1][j]||dp[i][j-1];
}
}
}else {
dp[i][j] = false;
}
}
}
}
return dp[rows][columns];
}
}