定义一个整型数组a,数组的长度n通过键盘输入,并通过键盘给数组a赋值,赋值后完成下列操作:
1.一次性输出整个数组a的元素
2.将数组a的数据复制到数组b中
3.对数组a进行排序,并一次性输出排序后数组a的元素
4.通过键盘输入一个数,判断该数是否存在数组中,如果存在输出该数所在的下标,不存在输入no
5.对数组a中的下标为0的元素到下标为2个(不包括2)数组元素,赋值为9,然后一次性输出整个数组的值
6.判断数组a和数组b是否相同(数组元素内容相同),如果相同输出yes,否则输出no
输入格式:
第一行输入数组a的长度
第二行输入数组a的各个元素(用空格隔开)
第三行输入要查询的元素
输出格式:
对每一组输入,在第一行输出数组a。
第二行输出数组b
第三行输出排序后的数组a
第四行输出查询后结果
第五行输出填充数据后的数组a
第六行输出两个数组判断的结果
输入样例:
在这里给出一组输入。例如:
5
2 1 3 5 6
7
5
4 3 7 6 8
7
输出样例:
在这里给出相应的输出。例如:
[2, 1, 3, 5, 6]
[2, 1, 3, 5, 6]
[1, 2, 3, 5, 6]
no
[9, 9, 3, 5, 6]
no
[4, 3, 7, 6, 8]
[4, 3, 7, 6, 8]
[3, 4, 6, 7, 8]
3
[9, 9, 6, 7, 8]
no
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[] = new int[n];
int b[] = new int[n];
for(int i=0;i<n;i++)
{
a[i] = sc.nextInt();
b[i] = a[i];
}
// output a&b
System.out.println(Arrays.toString(a));
System.out.println(Arrays.toString(b));
// sort output
BubbleSort(a);
System.out.println(Arrays.toString(a));
// find
int m= Arrays.binarySearch(a,sc.nextInt());
if(m>-1) System.out.println(m);
else System.out.println("no");
// assign
if (n > 0)
{
a[0] = 9;
}
if (n > 1)
{
a[1] = 9;
}
System.out.println(Arrays.toString(a));
// judge
boolean jud = judge(a,b);
if(jud) System.out.println("yes");
else System.out.println("no");
}
public static void BubbleSort(int arr[])
{
boolean judge = true;
for(int i=0;i<arr.length-1;i++)
{
for(int j=0;j<arr.length-1-i;j++)
{
if(arr[j]>arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
judge = false;
}
}
if(judge) break;
}
}
public static void find(int x,int arr[])
{
boolean jud = false;
int i=0;
for(;i<arr.length;i++)
{
if(arr[i]==x)
{
jud = true;
break;
}
}
if(jud) System.out.println(i);
else System.out.println("no");
}
public static boolean judge(int arr1[],int arr2[])
{
if(arr1.length!=arr2.length) return false;
boolean jud = true;
for(int i=0;i<arr1.length;i++)
{
if(arr1[i]!=arr2[i])
{
jud = false;
break;
}
}
return jud;
}
}