Typical Java exercises (3)

Source: Internet
Author: User

11. the ancient Chinese computation book Zhang qiujian computing Sutra has a famous hundred rooster problem: Each rooster is worth 5 RMB, and each hen is worth 3 RMB, the value of three chicks is 1 RMB. I used 100 yuan to buy 100 chickens. Q: How many of the 100 chickens are there?

Program Analysis: this problem needs to be solved by nested for loop. x indicates the number of Rooster, y indicates the number of hens, and z indicates the number of chicks. X, y, and z are all loops from 0 to 100. three conditions must be met: a) x + y + z = 100; B) 5 * x + 3 * y + z/3 = 100; c) z % 3 = 0.

Public class Question11 {public static void main (String [] args) {for (int x = 1; x <= 20; x ++) {for (int y = 1; y <= 33; y ++) {for (int z = 1; z <= 100; z ++) {if (z % 3 = 0) & (x + y + z = 100) & (5 * x + 3 * y + z/3 = 100) {System. out. println ("rooster has" + x + "only, hen has" + y + "only, chicken has" + z + "only. ");}}}}}}


12. The program receives a positive integer n.

If n = 3, print

*

***

*****

If n = 4, print

*

***

*****

*******

Program analysis: the positive integer n received by the program represents the number of rows. The problem is how many * numbers should be printed for row I. We can find a rule, the number of * numbers printed in each line is accumulated by 2 each time.

Import java. util. optional; public class Question12 {public static void main (String [] args) {int n; // represents the total number of rows int m =-1; // indicates the number of * numbers in each row. System. out. println ("enter a positive integer n:"); then SC = new then (System. in); n = SC. nextInt (); for (int I = 1; I <= n; I ++) {m = m + 2; for (int j = 1; j <= m; j ++) {System. out. print ("*");} System. out. println ();}}}

13. Write a Java program to output the 99 multiplication table.

Program Analysis: First let's look at the 99 multiplication table, and then find the rule

1 × 1 = 1 1 × 2 = 2 2 × 2 = 4 1x3 = 3 2x3 = 6 3x3 = 9 1 × 4 = 4 2 × 4 = 8 3 × 4 = 12 4 × 4 = 16 1x5 = 5 2x5 = 10 3x5 = 15 4x5 = 20 5x5 = 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 7 × 7 = 49 1x8 = 8 2x8 = 16 3x8 = 24 4x8 = 32 5x8 = 40 6x8 = 48 7x8 = 56 8x8 = 64 1x9 = 9 2x9 = 18 3x9 = 27 4x9 = 36 5x9 = 45 6x9 = 54 7x9 = 63 8x9 = 72 9 × 9 = 81

Through the above table, we can easily find that there are a total of nine rows, and the row I of each row will be used as the multiplier, while the multiplier will change from 1 to I.

public class Question13 {public static void main(String[] args) {for (int i = 1; i <= 9; i++) {for (int j = 1; j <= i; j++) {System.out.print(" " + j + "*" + i + "=" + (i * j) + " ");}System.out.println();}}}

14. The program receives a positive integer n and outputs the sum of the numbers in each digit of n. (Note that n is an arbitrary number of digits)

Program Analysis: Because the int type represents a limited range of values, int cannot be used to record the input positive integer n. You can use a String type variable to record it, then, the numbers above each bit are read and accumulated through the for loop.

Public class Question14 {public static void main (String [] args) {System. out. println ("enter a positive integer n:"); then SC = new then (System. in); String str = SC. next (); int sum = 0; for (int I = 0; I <str. length (); I ++) {String ch = str. substring (I, I + 1); int temp = Integer. valueOf (ch); sum + = temp;} System. out. println (the sum of the individual digits of str + "is:" + sum );}}


15.

Calculate the circumference rate:

Ancient Chinese mathematicians have developed the simplest way to calculate the circumference rate:

PI = 4/1-4/3 + 4/5-4/7 + 4/9-4/11 + 4/13-4/15 + 4/17 ......

The result of this formula is infinitely close to the value of the circumference rate. The ancient mathematicians in China calculated that the circumference rate is at 3.1415926 and

Between 3.1415927, Please program the calculation. How many addition and subtraction operations does it take to get such a result?

Program Analysis: we need to use the while loop to find that the divisor is always 4, and the divisor increases by 2 at a time, so the program is as follows:

Public class Question15 {public static void main (String [] args) {final double a = 4.0; // represents the dividend double PI = 0; // represents the circumference rate int B =-1; // divisor int count = 0; // addition/subtraction count variable while (PI> = 3.1415927 | PI <= 3.1415926) {B + = 2; PI = PI + a/B; count ++; B + = 2; PI = PI-a/B; count ++;} System. out. println ("total needs to go through" + count + "subsubtraction operation ");}}



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.