Java exercise Question 1: Find all the daffodils in the-Number

Source: Internet
Author: User

Exercise: Find out all the daffodils in.

Analysis: the number of daffodils refers to a three-digit number.CubeAnd equals to the number itself. For example: 370 = 33 + 73 + 00. This indicates that 370 is a daffodils.

Therefore, to determine whether a three-digit number is a daffodils number, you must first obtain the three-digit number, ten digits and hundreds of digits. Here I will use the following methods:

1:

First, get his hundred bits: int Wei = x/100; Because int is an integer, it will not be rounded down or the number after the decimal point.

Ten more digits: int Shiwei = (X-Wei * 100)/10. Use this number to subtract the hundred digits * 100, and then divide it by 10 for the same reason.

Finally, obtain the single digit: int gewei = x-yaowei * 100-shiwei * 10. Obviously, you can obtain the single digit.

2:

Intb = x/0/100;

Int S = x/100% 10;

Int G = x % 10;

Method of cube calculation:

1, x = Wei * Wei + Shiwei * Shiwei + gewei * gewei2. The method for raising the power of supply and demand in Java is Java. lang. math. pow (a, B); it indicates the power B of.

LastProgram:

First:

 Public  Class  Shuixianhuashu {  Public   Static   Void  Main (string [] ARGs ){  Int X = 0; //  Define the number of daffodils          For ( Int I = 100; I <= 999; I ++ ){  Int B = I/100; //  Hundreds of BITs             Int S = (i-100 * B)/10; //  Dozens              Int G = (I-s * 10-B * 100 ); //  Obtain a single digit                          If (I = g * g + S * s + B * B * B) {x ++; //  X + 1 if the number of daffodils is met each time; System. Out. Print (I + ""); //  Number of output conforming Conditions  } System. Out. println (); //  Line feed System. Out. println ("daffodils total" + x + "); //  Total number of output daffodils  }} 

Second:

 Public   Class  Shuixianhuashu {  Public   Static   Void  Main (string [] ARGs ){  Int X = 0; //  Define the number of daffodils         For ( Int I = 100; I <= 999; I ++ ){  Int B = I/100; //  Hundreds of BITs              Int S = I % 100/10; //  Dozens              Int G = I % 10; //  Obtain a single digit                          If (I = math. Pow (B, 3) + math. Pow (s, 3) + math. Pow (G, 3)) {X ++; //  X + 1 if the number of daffodils is met each time; System. Out. Print (I + ""); //  Number of output conforming Conditions  } System. Out. println ();  //  Line feed System. Out. println ("daffodils total" + x + "); //  Total number of output daffodils  }} 

 

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.