[Java] exercise question 003: calculate the number of daffodils between 100 and 1000

Source: Internet
Author: User

[Procedure 3]
Question: print out all the "Daffodils". The so-called "Daffodils" refers to a three-digit number, each of which is equal to the number itself. For example, 153 is a "Daffodils" because 153 = the power of 1 + the power of 5 + the power of 3.
1. program analysis: The for loop is used to control the number of 100-999. Each number is used to calculate a single digit, ten digits, and hundreds of digits.

Import Java. util. *; <br/> public class daffodiltest <br/>{< br/> Public static void main (string [] ARGs) <br/>{< br/> int x = 0; <br/> vector v = new vector (); <br/> daffodilnumber DB = new daffodilnumber (); <br/> for (INT I = 100; I <1000; I ++) <br/>{< br/> If (dB. isdaffodilnumber (I) = true) <br/>{< br/> v. add (I); <br/> X ++; <br/>}< br/> system. out. println ("+ x +" Daffodils "between 100 and 1000); <br/> system. out. println ("All daffodils:" + V ); <br/>}< br/> class daffodilnumber <br/>{< br/> int B, S, G; <br/> Public Boolean isdaffodilnumber (int I) <br/>{< br/> Boolean flag = false; <br/> G = I % 10; // single digit <br/> S = (I/10) % 10; // ten digits <br/> B = I/100; // hundreds of digits <br/> if (I = (g * g) + (S * s) + (B * B ))) <br/>{< br/> flag = true; <br/>}< br/> return flag; <br/>}< br/>

Related Article

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.