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