JavaSE classic programming questions

                                          JavaSE classic programming questions

                                                                                                                              Author: Tian extraordinary

All rights reserved, please indicate the original author, legal liability for counterfeiting and infringement

[Procedure 1] TestRabbit.java
Title: Classical problem: There are a pair of rabbits. From the third month after birth, a pair of rabbits will be born every month, and the rabbit will grow to a pair of rabbits every month after the third month. If the rabbits are not dead, what is the total number of rabbits each month? 

Program analysis: The law of rabbits is the sequence 1,1,2,3,5,8,13,21 ....

 

[Procedure 2] FindPrimeNumber.java

Topic: Determine how many prime numbers are between 101-200 and output all prime numbers. 

Program analysis: The method of judging prime numbers: divide 2 to sqrt (this number) by a number, if it can be divided by 

It means that this number is not a prime number, otherwise it is a prime number.

 

[Procedure 3] FindDaffodilNumber.java
title: print out all "narcissus number", the so-called "narcissus number" refers to a three-digit number, and the cubic sum of each digit is equal to the number itself. For example: 153 is a "narcissus number", because 153 = 1 cube + 5 cube + 3 cube. 

Program analysis: use for loop to control 100-999 numbers, each number is decomposed into singles, tens and hundreds.

 

[Program 4] FenJie.java

Topic: Factorize a positive integer. For example: enter 90 and print out 90 = 2 * 3 * 3 * 5. 

Program analysis: To decompose prime factors for n, you should first find a minimum prime number k, and then complete the following steps: 

(1) If this prime number is exactly equal to n, it means that the process of decomposing the prime factor has ended, just print it out. 

(2) If n <> k, but n is divisible by k, you should print out the value of k, and divide the quotient of n by k, as the new positive integer you n, repeat the first step. 

(3) If n is not divisible by k, then use k + 1 as the value of k and repeat the first step.

 

[Procedure 5] ConditionOperator.java

Topic: Use the nesting of conditional operators to complete this question: students with academic scores> = 90 points are represented by A, between 60-89 points are represented by B, and under 60 points are represented by C. 

Program analysis: (a> b)? A: b This is a basic example of conditional operators.

 

[Procedure 6] Test1.java GcdTest.java The latter is torn apart

Title: Enter two positive integers m and n and find their greatest common divisor and least common multiple. 

Analysis of the program: the use of the removal method.

 

[Procedure 7] StChar.java

Title: Enter a line of characters and count the number of English letters, spaces, numbers, and other characters. 

Program analysis: using the while statement, the condition is that the input character is not '\ n'.

 

[Procedure 8] TestAdd.java

Title: Find the value of s = a + aa + aaa + aaaa + aa ... a, where a is a number. For example, 2 + 22 + 222 + 2222 + 22222 (at this time there are a total of 5 numbers added), and several numbers added have keyboard control. 

Program analysis: The key is to calculate the value of each item.

 

[Program 9] WanShu.java

Topic: If a number is exactly equal to the sum of its factors, the number is called "final number". For example, 6 = 1 + 2 + 3. Program to find all the counts within 1000.

 

[Program 10] TestBall.java

Title: A ball falls freely from a height of 100 meters. After each landing, it jumps back to half of its original height; after falling again, how many meters did it pass when it landed for the 10th time? How high is the 10th rebound?

 

[Program 11] TestTN.java

Question: There are 1, 2, 3, and 4 numbers. How many three-digit numbers can be composed of different and non-repeating numbers? How much are they? 

Program analysis: The numbers that can be filled in the hundreds, tens, and ones are 1, 2, 3, and 4. After forming all the arrangements, remove the arrangements that do not satisfy the conditions.

 

[Program 12]
Title of MoneyAward.java : The bonuses issued by the enterprise are based on profits. When the profit (I) is less than or equal to 100,000 yuan, the bonus can be increased by 10%; when the profit is greater than 100,000 yuan, less than 200,000 yuan, the part less than 100,000 yuan is deducted at 10% and higher than 100,000 yuan The portion of the cocoa is 7.5%; between 200,000 and 400,000, the portion higher than 200,000 can be deducted 5%; between 400,000 and 600,000, the portion higher than 400,000 can be deducted 3% ; Between 600,000 and 1 million, the portion higher than 600,000 yuan can be deducted by 1.5%; when it is higher than 1 million yuan, the portion higher than 1 million yuan is deducted by 1%, enter the profit I of the month from the keyboard Total number of bonuses issued? 

Program analysis: Please use the number axis to divide and position. Pay attention to the need to define the bonus growth type.

 

[Program 13] FindNumber.java

Question: An integer, which is a perfect square after adding 100, and a perfect square when adding 168, what is the number? 

Program analysis: judge within 100,000, add 100 to the number before prescribing, then add 268 to the number before prescribing, if the result after prescribing meets the following conditions, it is the result.

[Program 14] TestDay.java

Title: Enter the day of the year, month and month, and determine whether this day is the day of the year? 

Program analysis: Take March 5 as an example, you should add up the first two months, and then add 5 days, which is the first few days of the year, special circumstances, if you enter a leap year and the month is greater than 3, you need to consider adding an extra day .

 

[Program 15] TestCompare.java
title: input three integers x, y, z, please output the three numbers from small to large. 

Program analysis: we think of a way to put the smallest number on x, first compare x and y, if x> y, then exchange the value of x and y, and then use x and z to compare, if x> z Then exchange the values ​​of x and z, so that x can be minimized. 

 

[Program 16] Nine.java

Title: Output 9 * 9 formulas. 

Program analysis: Branch and column consideration, a total of 9 rows and 9 columns, i control row, j control column.

 

[Program 17] MonkeyEatPeach.java

Topic: Monkey eating peach Problem: The monkey picked a few peaches on the first day, and immediately ate half of them, not addicted, and ate another one. The next morning, he ate half of the remaining peaches and ate one more . After that, every morning I ate the remaining half and one of the previous day. When I wanted to eat again on the morning of the 10th day, I saw only one peach left. Ask how many were picked on the first day. 

Program analysis: take the method of reverse thinking and infer from back to front.

 

[Procedure 18] Prog.java
title: Two table tennis teams play a match, each with three people. Team A is composed of a, b, and c, and team B is composed of x, y, and z. A lottery has been decided on the game list. Someone asked the players for a list of matches. a said he did not compare with x, c said he did not compare with x, z, please program to find the list of the three teams.

Program analysis: The method of judging the prime number: divide 2 to sqrt (this number) by a number, if it can be divided, it means that this number is not a prime number, otherwise it is a prime number.

 

[Program 19] LingXing.java 
topic: print out the following pattern (diamond) 
    *
  ***
 ******
********
 ******
   ***
     *
1. Program analysis: first The graph is divided into two parts. The first four rows have one rule, and the last three rows have one rule. Using a double for loop, the first layer controls rows and the second layer controls columns.

 

[Program 20] TestAdd2.java

Title: There is a sequence of scores: 2/1, 3/2, 5/3, 8/5, 13/8, 21/13 ... Find the sum of the first 20 items of this sequence. 

Program analysis: Please grasp the changing law of numerator and denominator.

 

[Program 21] TestJieCheng.java

Title: Sum of 1 + 2! +3! + ... + 20! 

Program analysis: This program just turns the accumulation into accumulation.

 

【Program 22】 

Title: Use recursive method to find 5! TestJieCheng.java

Program analysis: recursive formula: fn = fn_1 * 4!

 

[Program 23] TestAge.java

Title: There are 5 people sitting together, and how old is the fifth person? He said he was 2 years older than the fourth person. Asked the age of the fourth person, he said that he was 2 years older than the third person. Ask the third person and say that he is two years older than the second person. Ask the second person and say that he is two years older than the first person. Finally asked the first person, he said that he was 10 years old. How old is the fifth person? 

Program analysis: using recursive methods, recursion is divided into two stages: recursion and recursion. To know the age of the fifth person, you need to know the age of the fourth person, and so on, followed by the first person (10 years old), and then pushed back.

 

[Program 24] TestNumber.java

Title: Give a positive integer no more than 5 digits. Requirements: 1. Find a few digits. 2. Print out the digits in reverse order.

 

[Program 25] HuiWenShu.java

Question: A 5-digit number, determine whether it is a palindrome. That is, 12321 is the number of palindromes, the single digit is the same as the tens of thousands, and the ten digit is the same as the thousands.

 

[Program 26] Ex26.java

Title: Please enter the first letter of the day of the week to determine the day of the week. If the first letter is the same, continue to determine the second letter. 

Program analysis: It is better to use the case statement. If the first letter is the same, then judge the second letter using the case statement or if statement. 

[Program 27] ​​SuShu.java

Topic: Find the prime number within 100

 

[Program 28] TestSort.java

Title: Sort 10 numbers 

Program analysis: You can use the selection method, that is, from the last 9 comparisons, select the smallest one to exchange with the first element, and the next time, the second element will be compared with the last 8 and exchanged. 

 

[Program 29] TestAdd3.java

Topic: Find the sum of the diagonal elements of a 3 * 3 matrix 

Program analysis: use a double for loop to control the input two-dimensional array, and then accumulate a [i] [i] to output. 

 

[Program 30] ArraySort.java

Topic: There is an array that has been sorted. Now enter a number and request that it be inserted into the array as originally. 

 Program analysis: first determine whether this number is greater than the last number, and then consider the situation of inserting the middle number. After inserting, the number after this element is moved one position in turn. 

 

[Program 31] ArrayConverse.java

Title: Output an array in reverse order. 

Program analysis: exchange the first one with the last one. 

 

[Program 32] Ex32.java

Title: Take an integer a 4 to 7 digits from the right end. 

Program analysis: Think of it this way: 

(1) First shift a to the right by 4 bits. 

(2) Set a number whose lower 4 bits are all 1s and the rest are all 0s. Available ~ (~ 0 << 4)

(3) Perform the & operation on the above two. 

[Procedure 33] YangHui.java

Title: Print Yang Hui triangle (requires to print 10 lines as shown below) 

Program analysis: 

 

 

1
    1 1
   1 2 1
  1 3 3 1
 1 4 6 4 1
 

[Procedure 34] A little more complicated has been done before

Title: Enter 3 numbers a, b, c, and output in order of size. 

Program analysis: use the pointer method. 

 

[Program 35] ArrayChange.java

Topic: Input array, the largest exchanges with the first element, the smallest exchanges with the last element, and the output array. 

 

[Program 36] Array1.java

Title: There are n integers, so that the preceding numbers are sequentially shifted back by m positions, and the last m numbers become the first m numbers. 

 

[Program 37] Test3Quit.java

Title: There are n people in a circle, arranged in order. Counting from the first person (from 1 to 3), everyone who reported 3 withdrew from the circle and asked what was the last number left. 

 

[Program 38] TestLength.java

Topic: Write a function, find the length of a string, enter the string in the main function, and output its length. 

 

[Program 39] Test2.java

Topic: Write a function, when input n is even, call the function to find 1/2 + 1/4 + ... + 1 / n, and when input n is odd, call the function 1/1 + 1/3 + .. . + 1 / n (using pointer function).

 

[Program 40] Test3.java

Topic: String sorting. 

 

[Program 41] MonkeyPeach.java

Title: There are a bunch of peaches on the beach, five monkeys come to divide. The first monkey divided the pile of peaches into five parts. One more monkey threw one more into the sea and took one. The second monkey divided the remaining peaches into five equal parts, and one more. It also threw one more into the sea and took one. The third, fourth, and fifth monkeys did so. Yes, how many peaches are there on the beach? 

 

[Program 42] Test4.java

Title: 809 * ?? = 800 * ?? + 9 * ?? + 1

Among them, ?? represents two digits, the result of 8 * ?? is two digits, and the result of 9 * ?? is three digits. Find the two digits represented by ??, and the result after 809 * ??. 

 

[Program 43] Test5.java

Topic: Find the odd number that 0-7 can form. 

 

[Program 44] TestEven.java

Topic: An even number can always be expressed as the sum of two prime numbers. 

 

[Program 45] TestPrime9.java

Topic: Judging whether a prime number is divisible by several 9s 

 

[Program 46] TestString.java

Topic: Two string concatenation procedures 

 

[Program 47] TestPrint.java

Title: Read the integer value of 7 numbers (1-50), and each time a value is read, the program prints out the number * of the value. 

 

[Program 48] TestCode.java

Title: A company uses a public telephone to transmit data. The data is a four-digit integer, which is encrypted during the transmission process. The encryption rules are as follows: add 5 to each digit, and then replace the digit with the remainder divided by 10. Then swap the first and fourth places, and the second and third places. 

 

[Program 49] TestString2.java

Topic: Count the number of occurrences of substrings in a string 

 

[Program 50] TestStu.java

Title: There are five students, and each student has the results of 3 courses. Enter the above data from the keyboard (including student number, name, and results of the three courses), calculate the average score, and combine the original data and the calculated average The score is stored in the disk file "stud".

Published 102 original articles · won 10 · 70,000 views +

Guess you like

Origin blog.csdn.net/qq_30056341/article/details/105528167