[Blue Bridge Cup java_group C · volume from scratch] section 3 (attached), for loop exercises (data and graphics)

Posted by smclay on Sun, 02 Jan 2022 12:07:55 +0100

catalogue

1, Basic data

1. Fibonacci sequence

2. Daffodils number

3. A horse carries grain

4. Cross the intersection

5. Stack paper

2, Graphic text printing

1. Print square

2. Left right triangle

3. Right right triangle

4. Isosceles triangle

5. Inverted left right triangle

6. Inverted right right triangle

7. Diamond

8. Yang Hui triangle

9. 99 multiplication table (four directions at right angles)

1, Basic data

1. Fibonacci sequence

Fibonacci sequence, also known as golden section sequence, is also known as "rabbit sequence" because the mathematician Leonardo Fibonacci introduced it with rabbit breeding as an example;

*It refers to such a sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34
*The law is that starting from the third number, each number is equal to the sum of its first two numbers.

2. Daffodils number

Output all "daffodils". The so-called "daffodils" refers to a 3-digit number whose cubic sum of each digit is equal to the number itself

3. A horse carries grain

A horse carries two stones of grain, a middle horse carries one stone of grain, and two ponies carry one stone of grain. It takes 100 horses to carry 100 stones of grain. How to allocate it?

4. Cross the intersection

Suppose someone has 100000 cash You need to pay a fee every time you pass an intersection The payment rule is that when his cash is greater than 50000, he needs to pay 5% each time. If his cash is less than or equal to 50000, he needs to pay 5000 each time Please write a program to calculate how many times this person can pass this intersection.

5. Stack paper

The thickness of a piece of paper is about 0.08mm. How many times can it be folded in half to reach the height of Mount Everest (8848.13m)?

2, Graphic text printing

1. Print square

*  *  *  *  * 
*  *  *  *  * 
*  *  *  *  *
*  *  *  *  * 
*  *  *  *  * 

package Action;

public class demos {
	public static void main(String[] args) {
		for(int i=0;i<5;i++) {//External circulation
			for(int j=0;j<5;j++) {//Internal circulation
				System.out.print("* ");//The inner loop is executed once and one is printed*
			}
			System.out.println();//Line feed
		}
	}
}

2. Left right triangle

        *
        * *
        * * *
        * * * *
        * * * * *

package Action;

public class demos {
	public static void main(String[] args) {
		for(int i=1;i<=5;i++) {
			for(int j=0;j<i;j++) {
				System.out.print("*");
			}
			System.out.print("\n");
		}
	}
}

3. Right right triangle

                *
              * *
            * * *
          * * * *
        * * * * *

package Action;

public class demos {
	public static void main(String[] args) {
		for(int i=1;i<=5;i++){
	    	   for(int j=5;j>i;j--){//Space triangle
	    		   System.out.print(" ");
	    	   }
	    	   for(int k=0;k<i;k++){//Asterisk triangle
	    		   System.out.print("*");
	    	   }
	    	   System.out.print("\n");
	       }
	}
}

4. Isosceles triangle

             *
           * * *
         * * * * *
      * * * * * * *
    * * * * * * * * *

package Action;

public class demos {
	public static void main(String[] args) {
		for (int i = 1; i <= 5; i++) {
			for (int k = 5; k > i; k--) {
				System.out.print(" ");
			}
			for (int j = 0; j < 2 * i - 1; j++) {
				System.out.print("*");
			}
			System.out.print("\n");
		}
	}
}

 

5. Inverted left right triangle

         * * * * *
         * * * * 
         * * *  
         * * 
         * 

package Action;

public class demos {
	public static void main(String[] args) {
		for(int i=0;i<5;i++) {
			for(int j=5;j>i;j--) {
				System.out.print("*");
			}
			System.out.print("\n");
		}
	}
}

6. Inverted right right triangle

         * * * * * 
           * * * *
             * * *
               * *
                 *

package Action;

public class demos {
	public static void main(String[] args) {
		for(int i=0;i<5;i++) {
			for(int k=0;k<i;k++){
				System.out.print(" ");
			}
			for(int j=5;j>i;j--) {
				System.out.print("*");			
			}		
			System.out.print("\n");
		}
	}
}

7. Diamond

             *
           * * *
         * * * * *
      * * * * * * *
    * * * * * * * * *
       * * * * * * * 
         * * * * * 
           * * * 
             * 
 

package Action;

public class demos {
	public static void main(String[] args) {
		for (int i = 1; i <= 5; i++) {
			for (int k = 5; k > i; k--) {
				System.out.print(" ");
			}
			for (int j = 0; j < 2 * i - 1; j++) {
				System.out.print("*");
			}
			System.out.print("\n");
		}
		for (int i = 5; i > 0; i--) {
			for (int k = 5; k >= i; k--) {
				System.out.print(" ");
			}
			for (int j = 2 * i - 3; j > 0; j--) {
				System.out.print("*");
			}
			System.out.print("\n");
		}
	}
}

8. Yang Hui triangle

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
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
... ... ... ...
The numbers on the two waist sides of Yang Hui triangle are 1. From the third line, except the first number and the last number, the numbers at other positions are the sum of the two numbers on the top.

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter how many lines you want to print:");
        int n=sc.nextInt();
        getTriangle(n);
    }
    public static int[][] getTriangle(int n){
        int[][] arr=new int[n][n];      //Create a two-dimensional array with n rows and columns
        for (int  i = 0; i <arr.length ; i++) {
            for(int j=0;j<=i;j++){
                if(i==0||j==0||i==j){       //Judge if the number of rows and columns is 1, or the number of rows and columns is equal
                    arr[i][j]=1;            //Then assign a value of 1
                }else{        //Otherwise, the value of other positions = the value of the previous column of the previous row + the value of this column of the previous row
                    arr[i][j]=arr[i-1][j-1]+arr[i-1][j];
                }
            }
        }
        //Convenient 2D array
        for(int i=0;i<arr.length;i++){
            for(int j=0;j<=i;j++){
                System.out.print(arr[i][j]+"\t");
            }
            System.out.println();
        }
        return arr;
    }

9. 99 multiplication table (four directions at right angles)

package Action;

public class demos {
	public static void main(String[] args) {
		// Lower left corner right angle
		for (int i = 1; i <= 9; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print(j + "*" + i + "=" + i * j + " ");
			}
			System.out.print("\n");// Line feed
		}
		// Upper left corner right angle
		for (int i = 9; i >= 1; i--) {
			for (int j = 1; j <= i; j++) {
				System.out.print(j + "*" + i + "=" + i * j + " ");
			}
			System.out.print("\n");
		}
		// Lower right corner right angle
		for (int i = 1; i <= 9; i++) {// Control line
			// Used to control the number of spaces per line
			for (int k = 1; k <= 9 - i; k++) {
				System.out.print("\t");
			}
			for (int j = 1; j <= i; j++) {// Control column
				System.out.print(j + "*" + i + "=" + (i * j) + "\t");
			}
			System.out.println();
		}
		// Upper right corner right angle
		for (int i = 9; i >= 1; i--) {
			for (int k = 1; k <= 9 - i; k++) {
				System.out.print("\t");
			}
			for (int j = 1; j <= i; j++) {
				System.out.print(j + "*" + i + "=" + (i * j) + "\t");
			}
			System.out.println();
		}
	}
}

 

 

Topics: Java Algorithm