1、输入一行字符串如2 abc 123456789;2代表后面有几个字符串;要求对第一个字符串后面的字符串进行分割和自动补0操作,完成操作后的字符串数组中每个元素长度为8,按字典序升序排序。结果 12345678 90000000 abc00000。
package bishi;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
public class huawei {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
String s = in.nextLine();
in.close();
String[] str = s.split(" ");
List<String> ls = new ArrayList<>();
for (int i = 1; i < str.length; i++) {
int num = 0;
if ((str[i].length() % 8) != 0) {
num = (str[i].length() / 8) + 1;
} else {
num = str[i].length() / 8;
}
for (int j = 0; j < num; j++) {
StringBuilder sb = new StringBuilder();
for (int k = j * 8; k < (j + 1) * 8 && k < str[i].length(); k++) {
sb.append(str[i].charAt(k));
}
if (sb.length() != 8) {
int len = 8 - sb.length();
for (int k1 = 0; k1 < len; k1++) {
sb.append(0);
}
ls.add(sb.toString());
} else {
ls.add(sb.toString());
}
}
}
String[] strs = new String[ls.size()];
ls.toArray(strs);
Arrays.sort(strs);
for (int i = 0; i < strs.length; i++) {
if (i != strs.length - 1)
System.out.print(strs[i] + " ");
else
System.out.print(strs[i]);
}
}
}
2、输入一串带有字母,数字和括号(数字与左括号相邻)的字符串,其中括号中可以嵌套字符串,不需要考虑括号不匹配问题。要求数字n后面的字符串重复n次,并消除数字n和对应括号。最后将字符串倒序输出。
输入:z2[a3[c]]b
输出:bcccacccaz
package bishi;
import java.util.Scanner;
import java.util.Stack;
public class huawei {
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
String s=in.nextLine();
in.close();
String str = help(s);
System.out.print(reverse(str));
}
//先消除数字覆盖范围内最里面的一对括号,然后递归使用函数直至所有数字和括号消除
public static String help(String s) {
Stack<Integer> stack1=new Stack<>();//记录左括号之前的数字
Stack<Integer> stack2=new Stack<>();//记录左括号的下标
for(int i=0;i<s.length();i++) {
if(s.charAt(i)>='0' && s.charAt(i)<='9') {
stack1.add(s.charAt(i)-'0');
}
else if(isLKH(s.charAt(i))) {
stack2.add(i);
}
else if(isRKH(s.charAt(i))) {
int l=stack2.pop();
int r=i;
int num=stack1.pop();
String subs=s.substring(l+1,i);
String cur="";
for(int j=0;j<num;j++) {
cur+=subs;
}
s=s.substring(0,l-1)+cur+s.substring(r+1);
break;
}
}
if(!stack1.isEmpty())
s=help(s);
return s;
}
//判断是否是右括号
public static boolean isRKH(char c) {
if(c=='}' || c==']' || c==')') {
return true;
}
else
return false;
}
//对字符串进行逆序输出
public static String reverse(String s) {
int r=s.length()-1;
StringBuilder sb=new StringBuilder();
for(int i=r;i>=0;i--) {
sb.append(s.charAt(i));
}
return sb.toString();
}
}
3、输入一个n*m的矩阵,其中矩阵中每一个元素,代表该坐标上的海拔高度,最后输入两个坐标(X,Y),(Z,W),前者出发点,后者是终点。从出发点出发,下一个坐标的海拔高度一定要比前一个高,判断是狗能够到达终点,若能,输出路径数,如不能,输出为-1。只能前后左右行走,不能斜着走。
这题没有时间了,写的比较仓促。只ac了18.18%。
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
String s1=in.nextLine();
String[] s1s=s1.split(" ");
int n=s1s[0].charAt(0)-'0';
int m=s1s[1].charAt(0)-'0';
int[][] nums=new int[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
nums[i][j]=in.nextInt();
}
}
int X=in.nextInt();
int Y=in.nextInt();
int Z=in.nextInt();
int W=in.nextInt();
in.close();
int res=helper(nums,X,Y,Z,W);
System.out.print(res);
}
public static int helper(int[][] a,int x,int y,int z,int w) {
if(x<0 || x>=a.length || y<0 || y>a[0].length)
return 0;
if(a[x][y]>a[z][w])
return 0;
if(x==z && y==w) {
return 1;
}
int r=0;
if(x+1 <a.length && a[x+1][y]>a[x][y] ){
r=helper(a,x+1,y,z,w);
}
int l=0;
if(x-1 >=0 && a[x-1][y]>a[x][y] ){
l=helper(a,x-1,y,z,w);
}
int u=0;
if(y-1 >=0 && a[x][y-1]>a[x][y] ){
u=helper(a,x,y-1,z,w);
}
int d=0;
if(y+1 <a[0].length && a[x][y+1]>a[x][y] ){
u=helper(a,x,y+1,z,w);
}
return l+r+u+d;
}
}