对此一无所知。我正在解决一些与数组相关的查询,也就是当我遇到这个问题时。 它要求我将数组划分为用户输入的大小,以便数组的第一个值必须大于或等于数组的最后一个值。
我无法确定如何创建子数组。 例如。 X = 4 A = {0、4、10、6、15、9、18、35、40,-30,-90、99} 大小为4且第一个元素大于最后一个元素的子数组是{10,6,15,9},{18,35,40,-30},{35,40,-30,-90}
答案 0 :(得分:0)
尝试
for(int i =0; i < array.length-3; i ++){
// 3 can be replaced by x-1
if(array[i]>array [i+3]){
System.out.println(array [i] + array [i+1] +array [i+2] +array [i+3]);
// Instead of printing you can save the sub array or do what you prefer
}}
答案 1 :(得分:0)
import java.util.*;
class Demo {
public static void main(String arg[]) {
int A[]={0,4,10,6,15,9,18,35,40,-30,-90,99};
int X=4;
ArrayList<int[]> arr=new ArrayList<>();
for(int i=0;i<A.length-X;i++) {
if(A[i]>=A[i+X-1]) {
int temp[]=new int[X];
for(int j=0;j<X;j++) {
temp[j]=A[i+j];
}
arr.add(temp);
}
}
for(int[] temp:arr) {
for(int j=0;j<X;j++) {
System.out.print(temp[j]+" ");
}
System.out.println();
}
System.out.println("Done....");
}
}
答案 2 :(得分:0)
执行以下操作:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] arr = { 0, 4, 10, 6, 15, 9, 18, 35, 40, -30, -90, 99 };
int[][] subarrays;
int x = 4;
int arrIndex = 0;
int temp;
if (arr.length % x > 0) {
subarrays = new int[arr.length / x + 1][x];
} else {
subarrays = new int[arr.length / x][x];
}
for (int i = 0; i < subarrays.length; i++) {
for (int j = 0; j < subarrays[0].length && arrIndex < arr.length; j++) {
subarrays[i][j] = arr[arrIndex++];
}
if (subarrays[i][0] < subarrays[i][subarrays[0].length - 1]) {// swap if true
temp = subarrays[i][0];
subarrays[i][0] = subarrays[i][subarrays[0].length - 1];
subarrays[i][subarrays[0].length - 1] = temp;
}
}
System.out.println(Arrays.deepToString(subarrays));
}
}
输出:
[[6, 4, 10, 0], [35, 9, 18, 15], [99, -30, -90, 40]]
迭代版本:
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int len = 0;
do {
System.out.print("Enter the number of elements: ");
if (in.hasNextInt()) {
len = in.nextInt();
}
} while (len <= 0);
int[] arr = new int[len];
int negativeRandomizer[] = { 1, -1 };
for (int i = 0; i < arr.length; i++) {
arr[i] = negativeRandomizer[(int) (Math.random() * 2)] * (int) (Math.random() * 100);
}
System.out.println("The array to be divided into sub-arrays: ");
System.out.println(Arrays.toString(arr));
int x = 0;
do {
System.out.print("Enter the size of sub-array: ");
if (in.hasNextInt()) {
x = in.nextInt();
}
} while (x <= 0 || x > len);
int[][] subarrays;
if (arr.length % x > 0) {
subarrays = new int[arr.length / x + 1][x];
} else {
subarrays = new int[arr.length / x][x];
}
int arrIndex = 0;
int temp;
for (int i = 0; i < subarrays.length; i++) {
for (int j = 0; j < subarrays[0].length && arrIndex < arr.length; j++) {
subarrays[i][j] = arr[arrIndex++];
}
if (subarrays[i][0] < subarrays[i][subarrays[0].length - 1]) {// swap if true
temp = subarrays[i][0];
subarrays[i][0] = subarrays[i][subarrays[0].length - 1];
subarrays[i][subarrays[0].length - 1] = temp;
}
}
System.out.println("Sub-arrays: ");
System.out.println(Arrays.deepToString(subarrays));
}
}
示例运行:
Enter the number of elements: 18
The array to be divided into sub-arrays:
[-85, 69, 4, -62, 93, -28, -82, -23, -62, 69, 68, -21, 70, -6, -90, -62, 1, 26]
Enter the size of sub-array: 5
Sub-arrays:
[[93, 69, 4, -62, -85], [69, -82, -23, -62, -28], [68, -21, 70, -6, -90], [0, 1, 26, 0, -62]]