生活随笔
收集整理的這篇文章主要介紹了
7-49 数组乱炖 (10 分)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
定義一個(gè)整型數(shù)組a,數(shù)組的長度n通過鍵盤輸入,并通過鍵盤給數(shù)組a賦值,賦值后完成下列操作:
1.一次性輸出整個(gè)數(shù)組a的元素
2.將數(shù)組a的數(shù)據(jù)復(fù)制到數(shù)組b中
3.對數(shù)組a進(jìn)行排序,并一次性輸出排序后數(shù)組a的元素
4.通過鍵盤輸入一個(gè)數(shù),判斷該數(shù)是否存在數(shù)組中,如果存在輸出該數(shù)所在的下標(biāo),不存在輸入no
5.對數(shù)組a中的下標(biāo)為0的元素到下標(biāo)為2個(gè)(不包括2)數(shù)組元素,賦值為9,然后一次性輸出整個(gè)數(shù)組的值
6.判斷數(shù)組a和數(shù)組b是否相同(數(shù)組元素內(nèi)容相同),如果相同輸出yes,否則輸出no
輸入格式:
第一行輸入數(shù)組a的長度 第二行輸入數(shù)組a的各個(gè)元素(用空格隔開) 第三行輸入要查詢的元素
輸出格式:
對每一組輸入,在第一行輸出數(shù)組a。 第二行輸出數(shù)組b 第三行輸出排序后的數(shù)組a 第四行輸出查詢后結(jié)果 第五行輸出填充數(shù)據(jù)后的數(shù)組a 第六行輸出兩個(gè)數(shù)組判斷的結(jié)果
輸入樣例:
在這里給出一組輸入。例如:
5
2 1 3 5 6
7
5
4 3 7 6 8
7
輸出樣例:
在這里給出相應(yīng)的輸出。例如:
[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
.Arrays
;
import java
.util
.Scanner
;public class Main {public static void main(String
[] args
) {Scanner sc
= new Scanner(System
.in
);int n
= sc
.nextInt();if (n
>0){int[] a
=new int[n
];int[] b
=new int[n
];for (int i
= 0; i
< n
; i
++) {a
[i
]= sc
.nextInt();}System
.out
.println(Arrays
.toString(a
));System
.arraycopy(a
, 0, b
, 0, a
.length
);System
.out
.println(Arrays
.toString(b
));Arrays
.sort(a
);System
.out
.println(Arrays
.toString(a
));int m
= Arrays
.binarySearch(a
,sc
.nextInt());if(m
>-1) System
.out
.println(m
);else System
.out
.println("no");if (n
>=2){a
[0]=9;a
[1]=9;}System
.out
.println(Arrays
.toString(a
));if (Arrays
.equals(a
,b
)) System
.out
.println("yes");else System
.out
.println("no");}}
}
總結(jié)
以上是生活随笔為你收集整理的7-49 数组乱炖 (10 分)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。