Java program entry 50 questions analysis: 003

Source: Internet
Author: User

【Program3]

Question: print out all the "Daffodils". The so-called "Daffodils" refers to a three-digit number.CubeAnd equals to the number itself. For example, 153 is a "Daffodils" because 153 = the power of 1 + the power of 5 + the power of 3.

Program Analysis: first, the three-digit number 100-999. Second, you must calculate the number of each digit and the number equal to this number. The public int Ge (int n) can be obtained ); // obtain the number of public int Shi (int n); // obtain ten public int Bai (int n); // obtain a hundred digits.
 
Package endual; public class qanswer {public int shuixianhua (int n) {int Ge = This. ge (n); int Shi = This. shi (n); int Bai = This. bai (n); int res = Ge * Ge + Shi * Shi + Bai * Bai; If (n = res) {return N ;} return 0;} private int Ge (int n) {return n % 10;} private int Shi (int n) {return (N % 100)/10; // The number must be rounded up.} private int Bai (int n) {return N/100; // directly integer }}
 package endual; public class main ,.... How many public static void main (string [] ARGs) {// todo auto-generated method stub // int n = 8; qanswer Qu = new qanswer (); For (INT n = 100; n <= 999; n ++) {int res = Qu. shuixianhua (n); If (0! = Res) {system. Out. println (RES) ;}}}
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.