50 Classic Java Programming questions (directory)

Source: Internet
Author: User
Tags case statement greatest common divisor

50 Classic Java Programming questions (directory)

Title from: http://blog.sina.com.cn/s/blog_60fafdda0100wb21.html

"Program 1" Testrabbit.java
Title: Classical Questions: There are a pair of rabbits, from the 3rd month after birth a pair of rabbits each month, the rabbit long to the third month after the birth of a pair of rabbits, if the rabbit is not dead, ask each month the total number of rabbits?
1. Program Analysis: The law of Rabbits for the series 1,1,2,3,5,8,13,21 ....

"Program 2" Findprimenumber.java
Title: Determine the number of primes between 101-200 and the output of all primes.
1. Procedure analysis: The method of judging primes: to remove 2 to sqrt (this number) by a number, if it can be divided evenly,
It indicates that the number is not a prime and vice versa.

"Program 3" Finddaffodilnumber.java
Title: Print out all the "daffodils", the so-called "Narcissus number" refers to a three-digit number, its number of cubes and equal to the number itself. For example:
153 is a "narcissus number", because the 153=1 three times the square +5 of the three +3 Times Square.
1. Program Analysis: Use for loop control 100-999 number, each number decomposition out of bits, 10 bits, hundred.

"Program 4" Fenjie.java
Title: Decompose a positive integer into factorization. For example: Enter 90 and print out 90=2*3*3*5.
Program Analysis: The decomposition of n factorization, should first find a minimum prime number k, and then the following steps to complete:
(1) If the prime number is exactly equal to N, then the process of decomposing the factorization is finished and printed out.
(2) if n<>k, but n can be divisible by K, the value of k should be printed, and n divided by the quotient of K, as a new positive integer you n, repeat the first step.
(3) If n cannot be divisible by K, the first step is repeated with k+1 as the value of K.

"Program 5" Conditionoperator.java
Title: Use the nesting of conditional operators to complete this problem: the students of the study score >=90 with a, 60-89 points between the use of B, 60 points below the C expression.
1. Program Analysis: (a>b)? A:b This is a basic example of a conditional operator.

"Program 6" Gcdtest.java Method of dividing
Title: Enter two positive integers m and N to find their greatest common divisor and least common multiple.
1. Program Analysis: The use of rolling division.

"Program 7" Stchar.java
Title: Enter a line of characters, respectively, the number of English letters, spaces, numbers and other characters.
1. Program Analysis: Using the while statement, the condition is that the input character is not ' n '.

"Program 8" Testadd.java
Title: The value of S=A+AA+AAA+AAAA+AA...A, where a is a number. For example 2+22+222+2222+22222 (there are 5 numbers added at this time), several numbers are added with keyboard control.
1. Program Analysis: The key is to calculate the value of each item.

"Program 9" Wanshu.java
Title: If a number is exactly equal to the sum of its factors, this number is called the "end number". For example, 6=1+2+3. Programming to find all the finished numbers within 1000.

"Program 10" Testball.java
Title: A ball from the height of 100 meters of freedom falling, each landing back to the original height of half, and then down, to ask it to fall on the 10th time, the total number of meters? How high is the 10th time rebound?

"Program 11" Testtn.java
Title: There are 1, 2, 3, 4 numbers, can make up how many different and no repetition of the number of three digits? How much are they?
1. Program Analysis: Can be filled in the hundred, 10 digits, digit digits are 1, 2, 3, 4. Make all the permutations and then remove the permutations that do not meet the criteria.

"Program 12" Moneyaward.java
Title: The bonuses awarded by the Enterprise are based on the profit commission. Profit (I) less than or equal to $100,000, the bonus can be raised by 10%, the profit is higher than $100,000, less than $200,000, less than 100,000 of the portion of the 10% commission, higher than the portion of 100,000 yuan, Cocoa Commission 7.5%, 200,000 to 400,000, higher than 200,000 yuan of the portion, can commission 5% ; Between 400,000 and 600,000 is higher than the portion of 400,000 yuan, can commission 3%, 600,000 to 1 million, higher than 600,000 yuan portion, can commission 1.5%, higher than 1 million yuan, the portion of more than 1 million yuan by 1% Commission, from the keyboard input month profit I, the total bonus should be issued?
1. Program Analysis: Please use the axis to demarcation, positioning. Note that the bonus definition should be defined as the growth integral type.

"Program 13" Findnumber.java
Title: An integer, which plus 100 is a complete square number, plus 168 is a complete square number, what is the number?
1. Procedure analysis: In 100,000 to judge, the number plus 100 after the root, and then the number plus 268 after the root, if the result of the root satisfies the following conditions, that is the result. Please see the specific analysis:

"Program 14" Testday.java
Title: Enter a certain day of the year, judging the day is the first of the year?
1. Procedure analysis: Take March 5 as an example, should first add up the first two months, and then add 5 days that is the first day of the year, special cases, leap years and enter the month greater than 3 o'clock need to consider more than one day.

"Program 15" Sort.java
Title: Enter three integers x, y, Z, please put these three numbers from small to large output.
1. Procedure analysis: We find a way to put the smallest number on the X, first compare x with Y, if x>y the value of x and Y to Exchange, and then with X and Z to compare, if X>z will be x and Z of the value of the exchange, this can make x minimum.

"Program 16" Nine.java
Title: Output 9*9 formula.
1. Program Analysis: Branch and column considerations, a total of 9 rows 9 columns, I control row, J control column.

"Program 17" Monkeyeatpeach.java
Title: Monkey Eat Peach problem: The first day of monkeys to take off a number of peaches, immediately ate half, not addicted to, and eat a second morning and the remaining peach eaten half, and eat one more. Every morning after eating the rest of the day half of the zero one. When I want to eat again in the morning of the 10th day, I see only one peach left. For the first day to pick a total number.
1. Procedure analysis: Adopt the method of reverse thinking, infer from the backward forward.

"Program 18" Prog.java
Title: Two table tennis team to play, each out three people. Team A for A,b,c three, team B for X, Y, z three people. Have drawn lots to determine the contest list. Someone asked the team for a list of matches. A says he does not compare with X, C says he does not compare with x,z, please compile the procedure to find out the list of the three teams.
1. Program Analysis: The method of judging the prime number: The number is removed from 2 to sqrt (this number), if it can be divisible, it is not prime, the inverse is prime.

"Program 19" Lingxing.java
Title: Print out the case (diamond)
*
***
*****
*******
*****
***
*
1. Program Analysis: First the graphics into two parts to see, the first four lines a rule, after three lines a rule, the use of double for loop, the first layer control row, the second Level control column.

"Program 20" Testadd2.java
Title: There is a fractional sequence: 2/1,3/2,5/3,8/5,13/8,21/13 ... to find out the sum of the first 20 items of this series.
1. Procedure analysis: Please grasp the molecular and the denominator of the law of change.

"Program 21" Testjiecheng.java
Title: Seeking the 1+2!+3!+...+20! and
1. Program Analysis: This program just adds the accumulation into the multiplicative.

"Program 22" Testjiecheng2.java
Title: Using recursive method to find 5!.
1. Program Analysis: Recursive formula: fn=fn_1*4!

"Program 23" Testage.java
Title: 5 people sit together and ask how old is the five? He said he was 2 years older than the 4th one. Asked the age of 4th, he said he was 2 years older than the 3rd man. Asked the third person, and said that the 2nd People's Congress two years old. Ask the 2nd person, who says two years older than the first one. Finally asked the first person, he said is 10 years old. How old is the fifth one?
1. Procedure analysis: Recursion is divided into two stages, recursive and recursive, using recursive method. To know the age of the fifth person, you need to know the age of the fourth person, and so on, push to the first person (10 years old), and then push back.

"Program 24" Testnumber.java
Title: Give a positive integer not more than 5 digits, requirements: First, it is a number of digits, second, the reverse print out the figures.

"Program 25" Huiwenshu.java
Title: a 5-digit number, judging whether it is a palindrome number. That is, 12321 is a palindrome number, single-digit and million-bit the same, 10 bits and thousands of the same.

"Program 26" Ex26.java (skipped, good boring question AH)
Title: Please enter the first letter of the day of the week to determine the day of the week, if the first letter, then continue to judge the second letter.
1. Procedure analysis: Use the situation statement is better, if the first letter, then judge the case statement or if statement to determine the second letter.

"Program 27" Sushu.java (see "Program 2" Findprimenumber.java)
Title: Number of primes within 100

"Program 28" Testsort.java
Title: Sort 10 Numbers
1. Program Analysis: You can use the selection method, that is, from the next 9 comparison process, select a minimum and the first element exchange,
Two elements are compared with the latter 8 and exchanged.

"Program 29" Testadd3.java
Title: Finding the sum of diagonal elements of a 3*3 matrix
1. Program Analysis: Use the dual for loop control input two-dimensional array, and then add a[i][i] after the output.

"Program 30" Arraysort.java
Title: There is an array that has been sequenced. Now enter a number that requires it to be inserted into the array as it was originally.
1. Program Analysis: First determine whether this number is greater than the last number, and then consider inserting the middle number of cases, after inserting this element after the number,
Move back one position in turn.

"Program 31" Arrayconverse.java
Title: Output an array in reverse order.
1. Procedure analysis: With the first and the last exchange.

"Program 32" Ex32.java
Title: Take an integer A from the right end of the 4~7 bit.
Program Analysis: This can be considered:
(1) First move A to the right 4 bits.
(2) set a low 4 bit is all 1, the rest is all 0 of the number. Available ~ (~0<<4)
(3) Perform & operations on the above.

"Program 33" Yanghui.java
Title: Print out the Yang Hui triangle (requires 10 lines to print)
1. Program Analysis:
1
1 1
1 2 1
1 3 3 1
1 4 6) 4 1
1 5 10 10 5 1

"program 34" slightly ahead of the more complex already done
Title: Enter 3 number a,b,c, output by size order.
1. Procedure analysis: Using the pointer method.

"Program 35" Arraychange.java
Title: The input array, the largest exchange with the first element, the smallest with the last element interchange, the output array.

"Program 36" Array1.java
Title: There are n integers, so that they move backward in the order of the first number of M positions, the last m number becomes the first number of M

"Program 37" Test3quit.java
Title: There are n people around in a circle, order automatic arranging. Starting from the first person (from 1 to 3 off), where 3 of the people quit the circle, ask the last to stay
Is the number of the original number of the person.

"Program 38" Testlength.java
Title: Write a function to find the length of a string, enter a string in the main function, and output its length.

"Program 39" Test2.java
Title: Write a function, input n is even, call function to 1/2+1/4+...+1/n, when input n is odd, call function
1/1+1/3+...+1/n (using pointer functions)

"Program 40" Test3.java
Title: String sorting.

"Program 41" Monkeypeach.java
Title: There are a bunch of peaches on the beach, five monkeys to divide. The first monkey divided this pile of peaches into five copies, one more, and this monkey put more of a
Threw it into the sea and took a copy of it. The second monkey divided the remaining peaches evenly into five, and one more, and it threw many more into the sea.
, took a copy, the third, the fifth and the other monkeys are doing this, asked the beach at least how many peaches?

"Program 42" Test4.java
Title: 809*?? =800*?? +9*?? +1
Where?? The two-digit number represented, 8*?? The result is a two-digit number, 9*?? The result is a 3-digit number. Please?? The two-digit number represented, and 809*?? After the result.

"Program 43" Test5.java
Title: An odd number of 0-7 can be composed.

"Program 44" Testeven.java
Title: An even number is always expressed as the sum of two primes.

"Program 45" Testprime9.java
Title: Judging a prime number can be divisible by several 9

"Program 46" Teststring.java
Title: Two string connection programs

"Program 47" Testprint.java
Title: Reads an integer value of 7 numbers (1-50), each reading a value, the program prints out the number of *.

"Program 48" Testcode.java
Title: A company uses a public telephone to pass data, the data is a four-bit integer, is encrypted during delivery, encryption rules are as follows: each digit
Add 5, then replace the number with the remainder of 10, and then swap the first and fourth bits, second and third.

"Program 49" Teststring2.java
Title: Count the number of occurrences of a string's substrings

"Program 50" Teststu.java
Title: There are five students, each student has 3 courses of results, from the keyboard input above data (including student number, name, three lessons), calculated
The average score, the original data and the calculated average scores are stored in the disk File "stud".

50 Classic Java Programming questions (directory)

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.