山东科技大学 PTA(Java)7-10 数组乱炖

文章描述了一个Java程序,用于处理整型数组的操作,包括输入数组元素、排序、查找特定数值的索引、部分元素赋值以及比较两个数组是否相等。
摘要由CSDN通过智能技术生成

定义一个整型数组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;
	}
}

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值