JAVAJ Classic Program Programming 50 questions __ Programming

Source: Internet
Author: User
Tags gcd

 

Java BASIC Programming exercises 50 questions

In this paper, 50 Classic Java program to explain the detailed, for beginners can skip a number of logic too strong topics, such as the first problem with the method of recursion, beginners may not understand, it is best to see those who have if, for, while can be a simple solution to the problem. However, for more in-depth study of the students, or hope to be able to read at one breath, this is a more comprehensive thinking exercise.

"Program 1"

Title: Classical question: There are a pair of rabbits, from the 3rd month after the birth of a pair of rabbits each month, the rabbit long to the third month after the birth of a pair of rabbits each month, if the rabbit is not dead, ask the rabbit log every month for how much.

Procedure analysis: Rabbit's rule for series 1,1,2,3,5,8,13,21 ....

The best way to do this is to find the rules, like the high School series.

There are: a[n]=a[n-1]+a[n-1], and the first second is known, the following value can be obtained

Public class Programme1 {

Public static void main (string[] args) {

System. out. Print ("Please enter the month of the number of rabbits you want to know:");

Scanner scanner=New Scanner (System. in);

int n=scanner.nextint ();//Get input integer

System. out. println ("The first" +n+ "the total number of rabbits" +fun (n));

Scanner.close ();

}

The number of rabbits for the required month, and the number of rabbits to return the value.

private static int fun (intn) {

if (N==1 | | n==2)

return 1;

Else

return Fun (n-1) +fun (n-2);

}

}


"Program 2"

Title: Determine how many primes there are between 101-200, and output all primes.

Program Analysis:

* Prime number is: can only be divisible by 1 or itself, such as: 3,5,7,11,131 ...

* Method of determining prime numbers: Remove 2 to sqrt (this number) with a number,

* Actually use this number to remove 2 to his own less than 1 of the number can also be, but the calculation time increased

* If divisible, it indicates that this number is not prime, whereas it is prime.

Public class Programme2 {

Public static void main (string[] args) {

int sum=0;

for (inti = m < 200;i++) {

if (Isrightnum (i)) {//Judge whether this number is prime

System. out. Print (i+ "");

sum++;

if (sum%10==0) {//10 one line

System. out. println ();

}

}

}

System. out. println ("Integer of Prime number:" +sum);

}

The specific code that determines whether this number is prime

private Static Boolean Isrightnum (inti) {

for (intj = 2; J < math.sqrt (i); j + +) {

if (i%j==0) {//If divisible, it means not prime, can be interrupted immediately, continue to judge the next number

return false;

}

}

return true;

}

}


"Program 3"

Title: Print out all the "Narcissus number", the so-called "Narcissus number" refers to a three-digit number, its members of the digital cubic and equal to the number itself. For example: 153 is a "narcissus number", because the 153=1 three times side +5 of three times +3 of the three times side.

Program Analysis: Use for loop control 100-999 number, each number decomposition out of single-digit, 10, hundred.

Public class Programme3 {

Public static void main (string[] args) {

The total number of sum=0;//daffodils in int

for (inti = m < 1000;i++) {

intbite=i%10; Get a digit

intten=i/10%10; Get 10 bits

inthundred=i/100;//Get Hundred

If the number of Narcissus conditions is printed out

if (i== (Bite*bite*bite) +

(Ten*ten*ten) + (hundred*hundred*hundred)) {

System. out. Print (i+ "");

sum++;

}

}

System. out. println ("Total number of daffodils:" +sum);

}

}


"Program 4"

Title: Decompose a positive integer into decomposition. For example: Enter 90, print out 90=2*3*3*5.

Program analysis: N to decompose decomposition, you should find a minimum prime number k, and then complete the following steps:

(1) If this prime number is equal to n, then the process of decomposing decomposition is finished and printed out.

(2) if n<>k, but n can be divisible by k, you should print out the value of K, and n divided by the quotient of K, as a new positive integer n, repeat the first step.

(3) If n cannot be divisible by K, then repeat the first step using k+1 as the value of K.

Public class Programme4 {

Public static void main (string[] args) {

System. out. Print ("Please enter a positive integer you want to decompose:");

Scanner scanner=New Scanner (System. in);

int input=scanner.nextint ()//Get the number entered

System. out. println ();

System. out. Print (input+ "=");

for (inti = 2; i < input+1; i++) {

while (input%i==0&&input!=i) {

input=input/i;

System. out. Print (i+ "*");

}

if (input==i) {//None of the above is divisible, which means it is a prime number

System. out. Print (i);

break;

}

}

Scanner.close ();

}

}


"Program 5"

Topic: Use the nesting of the conditional operator to complete this problem: the students of the academic achievement >=90 score with a said, 60-89 points between the use of B, 60 points below the C-means.

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

Public class Programme5 {

Public static void main (string[] args) {

System. out. println ("Please enter your score:");

Scanner scanner=New Scanner (System. in);

int input=scanner.nextint ()//Get input

Grade Judgment

String belong=input>=90? " A ":(input>=60?" B ":" C ");

System. out. println (input+ "Cent belongs to:" +belong);

Scanner.close ();

}

}


"Program 6"

Title: Enter two positive integers m and n, and ask for their gcd and LCM.

Program Analysis: The use of rolling division.

* Here's a little bit of knowledge to remember, GCD and LCM.

* 1, first seek GCD bigdivisor

* 2, can be very convenient access to LCM Multiple=input1*input2/bigdivisor

* The most important thing here is to ask for GCD: The method is as follows

* (1) with a large number to the small number of the remainder

* (2) Assigning a small number to a large number, assigning the result of the remainder to a small number,

* (3) loop the previous operation until the result of the remainder is zero

* (4) The number of the last step is the gcd we want, if you don't believe it, you can try it.

Public class Programme6 {

Public static void main (string[] args) {

int bigdivisor=0;//definition gcd

int multiple=0;//definition LCM

System. out. println ("Please enter two integers:");

Scanner Scanner = new Scanner (System. in);

int input1 = Scanner.nextint ();//Get First number

int input2 = Scanner.nextint ();//Get second number

multiple=input1*input2;//this value to save, after the convention number, to facilitate the LCM

Intermediate number of int Temp =1;//Interchange

if (Input2 >input1) {//Ensure that the first number is not less than the second number

TEMP=INPUT1;

Input1=input2;

Input2=temp;

}

while (temp!=0) {//The remainder result is not equal to zero, it loops

temp=input1%input2;//The result of finding the remainder

input1=input2;//The big number is useless, with a small number to replace

input2=temp;//assigns the result of the remainder to a small number

}

bigdivisor=input1;//the last time the result is zero, the number of the remainder

Multiple=multiple/bigdivisor;

System. out. println ("GCD is:" +bigdivisor);

System. out. println ("LCM is:" +multiple);

Scanner.close ();

}

}


"Program 7"

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

Program Analysis:

Here's the need for knowledge points:

1, get a line of strings, nextline ()

2. Assignments the value of each character of the string into a number

3, compare each number in the Ask code range, you can determine the category of its symbols

4, char character ask code range

(1) Number 0 to 9:48~57

(2) Letter A to z:65 to z:97 to 122

(3) The space is 32

Public class Programme7 {

Public static void main (string[] args) {

int num=0;//number of digits

Number of int letter=0;//Letters

int space=0;//number of spaces

int others=0;//number of other

System. out. println (&qu

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.