Java數組中常見有點難度的練習題

來源:互聯網
上載者:User


1.取[0-n]的n個隨機數存入數組中,要求資料不能重複.

import java.util.Random;
import java.util.Scanner;
public class homework04_2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入n,將產生n個隨機數存入數組array[n]");
int n = sc.nextInt();
int a[] = new int[n];
suiJi(a,n);
}
public static void suiJi(int a[],int n)
{
Random r= new Random();
for(int i=0;i<n;i++)
{
int number = r.nextInt(n);
boolean s;
do{
s= false;
for(int j=0;j<i;j++)
{
if(number == a[j])
{
number = r.nextInt(n);
s=true;
break;
}
}
}while(s);
a[i]=number;
}
for(int j=0;j<n;j++)
{
System.out.println(a[j]);
}
}

}

1.2

public class homework04_2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入n,將產生n個隨機數存入數組array[n]");
int n = sc.nextInt();
int a[] = new int[n];
suiJi(a,n);
}
public static void suiJi(int a[],int n)
{
Random r= new Random();
int c=0;
Outer:while(c<n)
{
int s=r.nextInt(n);
for(int i=0;i<c;i++)
{
if(a[i]==s)
continue Outer;
}
a[c]=s;
c++;
}
for(int i=0;i<n;i++)
{
System.out.println(a[i]);
}
}


}

2. 設計一個方法,將數組中的資料打亂順序

import java.util.Random;


public class homework05 {
public static void main(String[] args) {
int n = 10;
int a[]=disorder(n);
for(int i=0;i<n;i++)
{
System.out.println(a[i]);
}

}
public static int[] disorder(int n)
{
Random r = new Random();
int a[] = new int [n];
for(int i=0;i<n;i++)
{
a[i] = i;
}
for(int i=0;i<n/2;i++)
{
int number = r.nextInt(n);
int temp = a[i];
a[i] = a[number];
a[number] = temp;
}
return a;
}
}

3.去除數組{1,3,1,4,2,3,6,1,5}中的重複項,存入一個新的數組,並從大到小排序.


public class homework08 {
public static void main(String[] args) {
int arr[] = { 1, 3, 1, 4, 2, 3, 6, 1, 5 };


int number = 0;
int count = 0;


for (int i = 0; i < arr.length - 1; i++) {
for (int j = 1; j < arr.length - i; j++) {
int temp = 0;
if (arr[j] > arr[j - 1]) {
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
}
}


}
for (int i = 0; i < arr.length; i++) {
number = arr[i];
boolean s = true;
for (int j = 0; j < i; j++) {
if (number == arr[j]) {
count++;
s=false;
}
}
if (!s) {
arr[i] = -1;
} else {
arr[i] = number;
}
}
int b[] = new int[arr.length - count];
int j = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] != -1) {
b[j] = arr[i];
j++;
}
}
for (int i = 0; i < b.length; i++) {
System.out.println(b[i]);
}


}
}

4.給定一個含有n個元素的整型數組a  例如{1,1,1,2,4,3,3} ,如果某些元素出現的次數為奇數次,則將其輸出:例如1,2,4


public class homework09 {
public static void main(String[] args) {
int a[] = { 1, 1, 1, 2, 4, 3, 3 };
int c[] = new int[a.length];
int number = 0;
int count = 0;
for (int q = 0; q < a.length; q++) {
c[q] = a[q];
// System.out.println(c[q]);
}


for (int i = 0; i < a.length; i++) {
boolean s = true;
number = a[i];


for (int j = 0; j < i; j++) {
if (number == a[j])
s = false;


}
if (!s) {
count++;
a[i] = -1;
} else {
a[i] = number;
}
}
int b[] = new int[a.length - count];
int count1 = 0;
for (int k = 0; k < a.length; k++) {
if (a[k] != -1) {
b[count1] = a[k];
count1++;
}
}
// for(int z=0;z<b.length;z++)
// {
// System.out.println(b[z]);
// }


for (int m = 0; m < b.length; m++) {
number = b[m];
int count2 = 0;
for (int n = 0; n < c.length; n++) {
if (number == c[n])
count2++;
}
if (count2 % 2 != 0) {
System.out.println(b[m]);
}
}


}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.