Java two-dimensional array application and compilation of Yang Hui triangle, two-dimensional array Yang Hui

Source: Internet
Author: User

Java two-dimensional array application and compilation of Yang Hui triangle, two-dimensional array Yang Hui

(1) write a program to generate a 10*10 two-dimensional random integer array, and save the maximum values of each row of the array in a one-dimensional array, store the average values of each column in another one-dimensional array and output them separately.

(2) programming outputs the first 10 lines of the Yang Hui triangle.

Find one, that is, the element at the position is the largest on the row and the element on the column is the smallest (Note: a two-dimensional array may not have such a saddle point ).

 

 

 

/**
*
* @ Author liuhui
* @ Version Java machine experiment 3
* @ Time 2016.10.30
*/

Public class javatest2 {
Public static int line = 0, row = 0;
Public static void main (String [] args) {// main Function
Int B [] [] = array ();
Int c [] = linearArray (B );
Double d [] = average (B );
PrintArrayOne (B );
System. out. println ();
PrintArrayTwo (c );
System. out. println ();
PrintArrayTwo (d );
System. out. println ();
Triangle aTriangle = new triangle ();

ATriangle. print ();
Int f [] = saddle (B, c );
For (int I = 0; I <f. length; I ++)
{
If (f [I] =-1)
Break;
Else
System. out. print (f [I] + "");
}

}

Public static int [] [] array () // obtain a random two-dimensional array
{
Int a [] [] = new int [10] [10];
For (int I = 0; I <10; I ++)
{
For (int j = 0; j <10; j ++)
{
Int r = (int) (Math. random () * 10 );
A [I] [j] = r;
}
}
Return;
}
Public static void printArrayOne (int a [] []) // print a two-dimensional array
{
For (int I = 0; I <10; I ++)
{
System. out. println ();
For (int j = 0; j <10; j ++)
{
System. out. print (a [I] [j]);
System. out. print ("");
}
}
System. out. println ();
}
Public static void printArrayTwo (int a []) // print a one-dimensional array
{
For (int I = 0; I <10; I ++)
{
System. out. print (a [I] + "");
}
}
Public static void printArrayTwo (double a [])
{
For (int I = 0; I <10; I ++)
{
System. out. print (a [I] + "");
}
}
Public static int [] linearArray (int a [] []) // calculates the maximum value of each array.
{
Int max = 0;

Int B [] = new int [10];
For (int I = 0; I <10; I ++)
{
For (int j = 0; j <10; j ++)
{
If (max <a [I] [j])
{
Max = a [I] [j];
Line = I;
Row = j;
}
}
B [I] = max;
Max = 0;
}
Return B;
}
Public static double [] average (int a [] []) // calculates the average number of arrays in each column
{
Double B [] = new double [10];
Double sum = 0.0;
For (int I = 0; I <10; I ++)
{
For (int j = 0; j <10; j ++)
{
Sum = sum + a [I] [j];
}
B [I] = sum/10;
Sums = 0.0;
}
Return B;
}
Public static int [] saddle (int a [] [], int B []) // judge the saddle Point
{
Int c [] = new int [10];
Int m = 0;
Boolean k = true;
For (int I = 0; I <10; I ++)
{
For (int j = 0; j <10; j ++)
{
If (B [I]> a [j] [row])
{
K = false;
Break;
}
}
If (k = true)
{
C [m] = B [I];
M ++;
}
}
If (k = false)
{
System. out. println ("No saddle point ");
C [0] =-1;
}
Return c;
}
}

 

Class triangle {// do Yang Hui triangle
Public void print ()
{
Int B [] [] = new int [10] [];
For (int I = 0; I <10; I ++)
B [I] = new int [I + 1];
For (int I = 0; I <10; I ++)
{
For (int j = 0; j <B [I]. length; j ++)
{
If (I = 0 | j = 0 | j = B [I]. length-1)
B [I] [j] = 1;
Else
B [I] [j] = B [I-1] [J-1] + B [I-1] [j];
}
}

For (int I = 0; I <10; I ++)
{
For (int k = 9-i; k> = 1; k --)
System. out. print ("");
For (int j = 0; j <I + 1; j ++)
{
System. out. print (B [I] [j] + "");
}
System. out. println ();
}

}
}

Related Article

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.