1. Java Basics (personal Summary) (4)

Source: Internet
Author: User
14.for Loop output Yang Hui's triangle

The Yang Hui's triangle is a numerical arrangement, and we can consider it as a numeric table whose basic properties are the sum of the digits on both sides of the value 1, the number at the other position, and the value of the upper left.

int [] [] triangle=new int[8][];

for (int i=0;i<triangle.length;i++) {//Row control

triangle[i]=New int[i+1];//declares a one-dimensional array for each row

for (int j=0;j<triangle[i].length;j++) {//Column control

if (i==0| | j==0| | J==TRIANGLE[I].LENGTH-1) {

Triangle[i][j]=1;

}Else{

TRIANGLE[I][J]=TRIANGLE[I-1][J]+TRIANGLE[I-1][J-1];

}

Output array element

System. out. Print (triangle[i][j]+ "\ t");

}

System. out. println ();

}

Results:

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

1 6 15 20 15 6 1

1 7 21 35 35 21 7 1

Note: The two-dimensional array in the Java language is actually a one-dimensional array of each element is another one-dimensional array, so the length of the second array can be arbitrary. 15.for Loop implementation 99 multiplication table

for (int i=0;i<9;i++) {//Row control

for (int j=0;j<=i;j++) {//Line control System. out. Print ((j+1) + "*" + (i+1) + "=" + (j+1) * (i+1) + "T");

}

System. out. println ();

}

The results are:

1*1=1

1*2=2 2*2=4

1*3=3 2*3=6 3*3=9

1*4=4 2*4=8 3*4=12 4*4=16

1*5=5 2*5=10 3*5=15 4*5=20 5*5=25

1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36

1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42

1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64

1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 16.for loop output Hollow Diamond

if (size%2==0) {

size++; Calculate the size of a diamond

}

for (int i=0;i<size/2+1;i++) {

for (int j=size/2+1;j>i+1;j--) {

System. out. Print ("");//output blank in upper left corner

}

for (int j=0;j<2*i+1;j++) {

if (j==0| | J==2*i) {

System. out. Print ("*");//output diamond upper half Edge

}Else{

System. out. Print ("");//output diamond Upper half Hollow

}

}

System. out. println ();

}

for (int i=size/2+1;i<size;i++) {

for (int j=0;j<i-size/2;j++) {

System. out. Print ("");//output diamond lower left corner blank

}

for (int j=0;j<2*size-1-2*i;j++) {

if (j==0| | j==2* (size-i-1)) {

System. out. Print ("*");//output diamond bottom half edge

}Else{

System. out. Print ("");//output diamond bottom half hollow

}

}

System. out. println ();

}

Set the size to 7, the result is:

*

* *

*   *

*    *

*   *

* *

*

17. Calculation of 1+1/2!+1/3!...... 1/20!

int I=1;

BigDecimal sum=New BigDecimal (0.0);

BigDecimal temp=New BigDecimal (1.0);

while (I<=20) {

Temp=temp.multiply (new BigDecimal (1.0/i));//Calculate factorial item

Sum=sum.add (temp);//Add the sum of the factorial

i++;

}

System. out. println ("1+1/2!+1/3!...... 1/20!= "+sum);

The results are:

1+1/2!+1/3!...... 1/20!=1.7182818284 ...

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.