题目,上图!!
上代码
import java.util.Scanner;
public class homework01 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of values: ");
int num = input.nextInt();
int[] values = new int[num];
System.out.print("Enter the values: ");
for (int i = 0; i < num; i++)
values[i] = input.nextInt();
if(isConsecutiveFour(values))
System.out.println("True");
else
System.out.println("Flase");
input.close();
}
public static boolean isConsecutiveFour(int[] values){
for (int i = 0; i < values.length - 4; i++){
boolean flag = true;
for(int j = 1; j < 4; j++)
if(values[i+j] != values[i]) {
flag = false;
break;
}
if(flag)
return true;
}
return false;
}
}
输出结果:
题目上图
上代码
import java.util.Scanner;
import java.util.Arrays;
public class homework02 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入第一个整数列表,注意第一位代表一共有几个数喔:");
System.out.println("请输入第二个整数列表,和第一个一样:");
String num1 = scan.nextLine();
String num2 = scan.nextLine();
String[] num1arr = num1.split(" ");
String[] num2arr = num2.split(" ");
int[] list1 = new int[Integer.parseInt(num1arr[0])];
int[] list2 = new int[Integer.parseInt(num2arr[0])];
for (int i = 1; i < num1arr.length; i++) {
list1[i-1] = Integer.parseInt(num1arr[i]);
}
for (int i = 1; i < num2arr.length; i++) {
list2[i-1] = Integer.parseInt(num2arr[i]);
}
int[] list3 = new int[list1.length + list2.length];
int p = 0, q = 0;
while (p < list1.length && q < list2.length) {
list3[p + q] = list1[p] < list2[q] ? list1[p++] : list2[q++];
}
if (p < list1.length) {
System.arraycopy(list1, p, list3, p + q, list1.length - p);
} else if (q < num2arr.length) {
System.arraycopy(list2, q, list3, p + q, list2.length - q);
}
System.out.println(Arrays.toString(list3));
}
}
输出结果: