Some of the most difficult exercises in Java arrays

Source: Internet
Author: User


1. The n random number of [0-n] is stored in an array, requiring that the data not be duplicated.

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 ("Please input n, will generate n random number into array 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 ("Please input n, will generate n random number into array 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. Design a method to disrupt the order of the data in the array

Import Java.util.Random;


public class Homework05 {
public static void Main (string[] args) {
int n = ten;
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. Removes duplicates from the array {1,3,1,4,2,3,6,1,5}, stores a new array, and sorts from large to small.


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. Given an integral array of n elements such as {1,1,1,2,4,3,3}, the output of some elements if they appear to be odd several times: for example, 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]);
}
}


}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.