Huawei Computer Testing Questions (upgraded version of daffodils-java), Huawei-java

Source: Internet
Author: User

Huawei Computer Testing Questions (upgraded version of daffodils-java), Huawei-java

PS: This question has a full score of 100 and is not correct. Please help us to see where the problem is.

/*
* Question: upgraded version of daffodils
* Description: The number of daffodils is an n-digit number (n ≥ 3). The sum of the n power of the number on each digit is equal to itself. (For example, 1 ^ 3 + 5 ^ 3 + 3 ^ 3 = 153)
Give you A and B, and find the number of daffodils in the [A, B] range.
 
Category: loop, search, enumeration, bitwise operation
Difficulty: Intermediate
Score: 100
Running time limit: Unlimited
Memory limit: Unlimited
Phase: apply for an exam
Input: two positive integers separated by spaces to ensure that all numbers are less than or equal to 1000000.
 
Output: A number indicating the number of daffodils in the range [A, B]
 
Sample input: 100 1000
 
Sample output: 4
 
Tip: 100 ~ 1000 of daffodils: 153,370,371,407
*/

 

 1 import java.util.Scanner; 2  3 public class Main { 4  5     public static void main(String[] args) { 6          7         int a = 0; 8         int b = 0; 9         int count = 0;10         11         Scanner s = new Scanner(System.in);12         String str = s.nextLine();13         String[] strArray = str.split(" ");14         a = Integer.parseInt(strArray[0]);15         b = Integer.parseInt(strArray[1]);16         s.close();17         if((a < 100 || a > 1000000) || (b < 100 || b > 1000000))18         {19             throw new RuntimeException();20         }21         22         count = getNum(a, b);23         24         System.out.println(count);25     }26 27     public static int getNum(int a, int b) {28         29         int result = 0;30         31         for(int i = a; i <= b; i++)32         {33             int m = i;34             int tmp = 0;35             int value = 0;36             37             while(0 != m)38             {39                 40                 tmp = m%10;41                 value += Math.pow(tmp, 3);42                 m /= 10;43             }44             45             if(value == i)46             {47                 result++;48             }49         }50         return result;51     }52     53 }

 


Java for loop: print out all the "Daffodils", the so-called "Daffodils" refers to a three-digit number, each of which is equal to the number itself

Public class sxh {
Public static void main (String [] agrs ){
For (int I = 100; I <1000; I ++ ){
Int a = I/100;
Int B = I/10% 10;
Int c = I % 10;
If (Math. pow (a, 3) + Math. pow (B, 3) + Math. pow (c, 3) = I)
System. out. println ("daffodils:" + I );
}

}
}

Java programming to find all the Daffodils (daffodils)

The so-called "Daffodils" refers to a three-digit number, each digit of which is equal to or equal
Itself. For example, 153 is a "Daffodils" because 153 = the power of 1 + the power of 5 + the power of 3.

Here is an example

Find all "Daffodils" between 100 and 999"
Find all "Daffodils" between 100--999 ". The so-called "Daffodils" refers to a three-digit number. The cubes and the numbers are equal to the bodies.

The key to solving this problem is how to separate hundreds, tens, and individual digits from a three-digit number. You can do this by setting the three numbers I, which are composed of three numbers a, B, and c.
(1) Hundreds of digits a: a = int (I/100 ).
(2) ten digits B: B = int (i-100 * a)/10)
(3) single digit c: c = I-int (I/10) * 10.

The Code is as follows:

Package com. vo;

Public class Shuixianhua {

Public static void main (String [] args ){

Int a = 0;
Int B = 0;
Int c = 0;
For (int I = 100; I <999; I ++)
{
A = I/100;
B = I/10% 10;
C = I % 10;
If (I = (a * a + B * B + c * c ))
System. out. println (I );
}

}

}
Reference: blog.sina.com.cn/s/blog_4889a88e010008bv.html

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.