Java Programming Ideas 4th Edition fourth Chapter exercise 10

Source: Internet
Author: User

Title: Vampire numbers are numbers with an even number of digits that can be multiplied by a pair of numbers, which each contain a number of half-digits of the product, where the number selected from the initial number can be arbitrarily sorted. Numbers ending in two 0 are not allowed, for example, the following numbers are "vampire" numbers:

1260=21*60

1827=21*87

2187=27*81

Write a program to find out 4-digit numbers of all vampires.

/** * <p> Vampire numbers </p> * @author Shockang * */public class Thevampirenumbers {/** * <p> determine if a 4-digit number is a vampire number method &L t;/p> * @param num * @return The first digit of the Boolean */private static boolean isvampire (int num) {int N1=NUM/1000;//4 number of digits int n2= (num -n1*1000)/100;//Second digit int n3= (num-n1*1000-n2*100)/10;//third digit int n4=num-n1*1000-n2*100-n3*10;//digit//                     Determines whether the number multiplied by numbers is the original number if ((N1*10+N2) * (n3*10+n4) ==num | |                     (N1*10+N2) * (n4*10+n3) ==num | |                     (N2*10+N1) * (n3*10+n4) ==num | |    (N2*10+N1) * (n4*10+n3) ==num | |                     (N1*10+N3) * (n2*10+n4) ==num | |                     (N1*10+N3) * (n4*10+n2) ==num | |                    (N3*10+N1) * (n2*10+n4) ==num | |    (N3*10+N1) * (n4*10+n2) ==num | |                    (N1*10+N4) * (n2*10+n3) ==num | |                     (N1*10+N4) * (n3*10+n2) ==num | |                     (N4*10+N1) * (n2*10+n3) ==num | | (N4*10+N1) * (N3*10+N2) ==num) {return true;} Else{return false;}} public static void Main (string[] args) {for (int i=1000;i< 10000;i++) {if (Isvampire (i)) {System.out.println (i);}}}} 

  

Java Programming Ideas 4th Edition fourth Chapter exercise 10

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.