题目难度中等偏低,第三题有点bug
第一题:写一个函数,传入两个非降序的整数数组(A, B),将 A, B 合并成一个非降序数组 C,返回 C(不要使用内置 sort 函 数)。
直接用sort一把梭哈,能过就行
public static ArrayList<Integer> solution(ArrayList<Integer> arr0, ArrayList<Integer> arr1, ArrayList<Integer> arr2){
ArrayList<Integer> result = new ArrayList<>();
for(int i = 0;i<arr1.size();i++){
result.add(arr1.get(i));
}
for(int i = 0;i<arr2.size();i++){
result.add(arr2.get(i));
}
Collections.sort(result);
// TODO: 请在此编写代码
return result;
}
第二题:已知n个整数。 每3个数算一个周期。 不足一个周期补0. 在周期的第一个位置的数的权值和等于a1+a[1+3]+… … 第二 个位置的数的权值和等于a2+a[2+3]+… … 求三个数的最大权值。 如果最大权值为第一个数,输出’J’; 最大权值为第二 个数,输出’H’; 最大权值为第三个数,输出’B’。
public static String solution(int n, ArrayList<Integer> arr){
String result = "";
int a1 = 0;
int a2 = 0;
int a3 = 0;
int pi = 0;
while(pi < arr.size()){
a1+=arr.get(pi);
pi+=3;
}
pi = 1;
while(pi < arr.size()){
a2+=arr.get(pi);
pi+=3;
}
pi = 2;
while(pi < arr.size()){
a3 += arr.get(pi);
pi+=3;
}
if(a1>a2 && a1 > a3){
result = "J";
}
if(a2>a1 && a2 > a3){
result = "H";
}
if(a3 > a1 && a3 > a2){
result = "B";
}
// TODO: 请在此编写代码
return result;
}
}
第三题:逆波兰记法中,操作符置于操作数的后面。例如表达“三加四”时,写作“3 4 +”,而不是“3 + 4”。如果有多个操作符,操 作符置于第二个操作数的后面,所以常规中缀记法的“3 - 4 + 5”在逆波兰记法中写作“3 4 - 5 +”:先3减去4,再加上5。 使用逆波兰记法的一个好处是不需要使用括号。例如中缀记法中“3 - 4 * 5”与“(3 - 4)*5”不相同,但后缀记法中前者写 做“3 4 5 * -”,无歧义地表示“3 (4 5 *) -”;后者写做“3 4 - 5 *”。
题目有点bug应该啊栈顶第二个减去第一个才对
class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String arr = cin.nextLine();
System.out.println(solution(arr));
}
public static int solution( String arr){
int result = 0;
// TODO: 请在此编写代码
String a[] = arr.split(" ");
Stack<Integer> st = new Stack<Integer>();
for(int i = 0;i<a.length;i++){
if(a[i].equals("+")){
int a2 = st.pop();
int a1 = st.pop();
st.add(a1+a2);
}
else if(a[i].equals("-")){
int a2 = st.pop();
int a1 = st.pop();
st.add(a2-a1);
}
else if(a[i].equals("*")){
int a2 = st.pop();
int a1 = st.pop();
st.add(a1*a2);
}
else if(a[i].equals("/")){
int a2 = st.pop();
int a1 = st.pop();
st.add(a2/a1);
}
else{
st.add(Integer.valueOf(a[i]));
}
}
result = st.pop();
return result;
}
}
第四题:开会了!作为一个集体,开会的时候桌子当然是需要和老大相邻的!(老大可能坐在桌子上边) 小艺被分配到排桌椅的活, 可是小艺的力气都用在吃上了,怎么可能搬动这些桌椅呢。 她决定用现有的布局当作是会议座位安排。 每个桌子分配一个 人。互相连接相同字符表示一个桌子,如UVV表示2张桌子,UVU则表示3张桌子。 小艺想知道选择某个桌子之后老大身 边能围多少人?
搜索题,没问题
class Main {
public static int xy[][] = {{-1,0},{0,-1},{1,0},{0,1}};
public static boolean is[][];
public static char mp[][];
public static int n,m;
public static void finish(int x,int y){
is[x][y] = true;
for(int i = 0;i<4;i++){
int nx = x + xy[i][0];
int ny = y + xy[i][1];
if(nx >= 0 && ny >= 0 && nx < n && ny < m && !is[nx][ny] && mp[x][y] == mp[nx][ny]){
finish(nx,ny);
}
}
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String p = cin.nextLine();
String a[] = p.split(" ");
n = Integer.valueOf(a[0]);
m = Integer.valueOf(a[1]);
char boss = a[2].charAt(0);
mp = new char[n][m];
is = new boolean[n][m];
for(int i = 0;i<n;i++){
p = cin.nextLine();
for(int j = 0;j<m;j++){
mp[i][j] = p.charAt(j);
}
}
int ans = 0;
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
if(mp[i][j] == boss){
for(int k = 0;k<4;k++){
int nx = i+xy[k][0];
int ny = j+xy[k][1];
if(nx>=0 && ny >= 0 && nx < n && ny < m && !is[nx][ny] && mp[nx][ny] != '.' && mp[nx]
[ny] != boss){
ans++;
finish(nx,ny);
}
}
}
}
}
System.out.println(ans);
}
}