Java Final Exam

Source: Internet
Author: User


One:

Title: Print out 100-999 of 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.

Code:

 Packagedemo; Public classShuixianhua { Public Static voidMain (string[] args) { for(inti=100;i<=999;i++){        inta,aa,aaa; AAA=i/100;//HundredAa= (i%100)/10;//10 Guestsa=i%10;//Digit        if(i== (aaa*aaa*aaa+aa*aa*aa+a*a*a))        {System.out.println (i); }    }    }}



Two: prime numbers that print 1----999

  

 Packagedemo; Public classPrime number { Public Static voidMain (string[] args) {BooleanFlag//Identity Bits         for(inti = 2; i < 1000; i++) {//Find a prime number 1-999Flag =true;  for(intj = 2; J < I; J + +) {                if(i% j = 0) {//according to the definition of prime number, only allow itself and 1 to be divisibleFlag =false;  Break;//Jump out of the inner layer loop                }            }            if(Flag = =true) {System.out.println (i); }        }    }}

Three: recursive algorithm to achieve factorial

 Packagedemo; Public classFactorial { Public Static voidMain (string[] agrs) {System.out.println (factorial (17));//calculates the factorial of 17} Public Static LongFactorial (inti) {    if(i==1){        return1; }Else{        returnI*factorial (i-1);//Recursive Run    }}}

Four: The title: A ball from 100 meters height 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?

 Packagedemo; Public classFreefall { Public Static voidMain (string[] args) {Doubleh=100;//Division to High Precision    DoubleSum=0;  for(inti = 1; i<=10; i++) {sum+=h;//the length of falling downH=h/2; if(i!=10) {//The tenth time does not bounce on the floor.Sum+=h;//the length of the projectile.}} System.out.println (Sum+","+h);}}

Java Final Exam

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.