Application of Java's two-dimensional array and writing of Yang Hui Triangle

Source: Internet
Author: User

(1) write a program that generates an array of two-dimensional random integers of 10*10 and stores the maximum values of each row of the array in a one-dimensional array, storing the average of each column in another one-dimensional array and outputting them separately.

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

Find out that the element on that position is the largest on the row and the smallest on that column (note: A two-dimensional array may not have such a saddle point).

/**
*
* @author Liuhui
* @version Java on the computer experiment three
* @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 ()//Get 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 A;
}
public static void Printarrayone (int a[][])//Print 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 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[][])//Find the maximum value of each row 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[][])//Find the average of each column array
{
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;
sum = 0.0;
}
return b;
}
public static int []saddle (int a[][],int b[])//Judging 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 ();
}

}
}

Application of Java's two-dimensional array and writing of Yang Hui Triangle

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.