Java BASIC Programming 50 questions (1-3 questions) detailed

Source: Internet
Author: User
Tags square root

I. Description of the topic

1, Classical problems: There is a pair of rabbits, from the 3rd month after birth, every month, a pair of rabbits, the rabbit long to the third month after the birth of a pair of rabbits, if the rabbit is not dead, ask the number of rabbits each month?

Program Analysis: The law of Rabbits for the series 1,1,2,3,5,8,13,21 .... , the problem is actually one of the Fibonacci sequences.

2, judge how many primes between m-n, and output all prime numbers.

Program Analysis: The method of judging primes: using a number n respectively to remove 2 to sqrt (n), here is the function of the math sqrt () to find the square root of the number, if it can be divisible, it indicates that the number is not a prime, the inverse is a prime.

3, print out all the "Narcissus number", so-called "Narcissus number" refers to a three-digit number, the number of its members of the cubic 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.

Program analysis: Using the For loop control 100-999 number, each number decomposition out of bits, 10 bits, hundred, and then judge whether to meet the number of the cube and equal to the number itself.


Second, Java source code

1. Procedure 1

Package Tong.yue.hong;import Java.util.scanner;import javax.xml.transform.templates;/** * Title: Classical question: There is a pair of rabbits, From the 3rd month after the birth of a pair of rabbits each month, the rabbit grew to the third month after the birth of a pair of rabbits, if the rabbit is not dead, ask the number of rabbits each month? Program Analysis: The law of Rabbits for the series 1,1,2,3,5,8,13,21 .... In short, except for the first and second months the number of rabbits is 1, the other month is the sum of the first two months of the rabbit logarithm. * @author Tong * */public class Fibonacci {public static void main (string[] args) {System.out.println ("Please enter months:"); Scanner Scanner = new Scanner (system.in); String month = Scanner.nextline ();d Iedai (integer.parseint (month));d Igui (integer.parseint (month)); System.out.println ("+month+", "+digui" (Integer.parseint (month)) + "Pair");} /** * Use recursive method to find the sequence, the source code is simple, but the efficiency is low, memory consumption is large * @param parseint * @return */private static int Digui (int parseint) {if (parseint==1 || parseint==2) {return 1;} Else{return Digui (parseInt-1) +digui (parseInt-2);}} /** * Using the normal method, according to the characteristics of the series, in addition to the first and second months the number of rabbits is 1, the other month is the sum of the first two months of the rabbit logarithm. * @param parseint */private static void Diedai (int parseint) {int first = 1;int Second = 1;int temp = 0;for (int i = 3; I <=parseInt; i++) {temp = First;first = Second;second =Temp+second; System.out.println ("+i+" Month has Rabbit "+second+" to ");}}



2. Program Two


Package Tong.yue.hong;import java.util.scanner;/* * Title: Determine how many primes are in between m-n and output all primes. Program Analysis: The method of judging primes: to remove 2 to sqrt (this number) with a number, if divisible, indicates that the number is not a prime, and vice versa is a prime. */public class Sushu {public static void main (string[] args) {Scanner Scanner = new Scanner (system.in); System.out.println ("Please enter the lower limit of the number:"); int low = Scanner.nextint (); System.out.println ("Please enter a number cap:"); int high = Scanner.nextint (); if (Low>high) {System.out.println ("you entered the wrong data, please re-enter!") "); System.out.println ("Please enter the lower limit of the number:"); low = Scanner.nextint (); System.out.println ("Please enter a number cap:"); high = Scanner.nextint ();} SYSTEM.OUT.PRINTLN (all primes between low+ "-" +high+ "); int count = 0;for (int i = low, I <= high; i++) {if (IsPrime (i)) {count + +; System.out.print (i+ "");//10 primes per output for easy observation of if (count%10==0) {System.out.println ();}}} System.out.println ("\ n Total" +count+ "number of primes");} /** * This method is used to determine whether a number is a prime, judging by dividing the number from 2 to sqrt (the number), as long as there is a divisible is not a prime number, all numbers can not be divisible by the prime numbers * @param i * @return */private static Boolean IsP rime (int i) {if (i==1) {return false;} for (int j = 2; J < math.sqrt (i); j + +) {if (i%j==0) {return false;}} return true;}}


3. Procedure 3

Package tong.yue.hong;/** * Prints out all the "daffodils", the so-called "daffodils number" refers to a three-digit number, whose numbers are cubic 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. Program Analysis: Use for loop control 100-999 number, each number decomposition out of bits, 10 bits, hundred. * @author Administrator * */public class Shuixianhua {public static void main (string[] args) {System.out.println ("100-999 The number of daffodils between: ");//Use Isshuixianhua () method for (int i = +, I < i++) {if (Isshuixianhua (i)) {System.out.print (i+" ");}} System.out.println ("Number of daffodils between \n100-999:");//Use the Islotus () method for (int i = +; i <; i++) {if (Islotus (i)) { System.out.print (i+ "");}}} /** * Here call Java's own POW () method to find a number of 3 times * @param i * @return */private static Boolean isshuixianhua (int i) {//Get bits: i%10, get 10 bits: I/10 %10, get the Hundred: i/100int sum = (int) (Math.pow (i%10, 3) +math.pow (I/10%10, 3) +math.pow (i/100, 3)); if (i==sum) {return true;} return false;} /** * Here the numbers on each bit are taken out and judged based on your data relationship * @param num * @return */private static boolean islotus (int num) {int m = 0;int n = num;int sum = 0;//Gets the hundred m = n/100;//source data minus the number of hundreds multiplied by 100 leaves 10 bits and a bit n-= m*100;//hundred numbers for cubic sum = m*m*m;//gets 10 bits m = n/10;//data minus 10 bits ofPart of the left bits of data n-= m*10;//judge the number of the cube and equals the number itself sum + = M*m*m + n*n*n;if (sum==num) return True;elsereturn false;} 


Java BASIC Programming 50 questions (1-3 questions) detailed

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.