问题:吸血鬼数字是指位数为偶数的数字,可以由一对数字相乘而得到,而这对数字各包含乘积的一半位数的数字,其中从最初的数字中选取的数字可以任意排序。以两个0结尾的数字是不允许的,例如,下列数字都是“吸血鬼”数字;1260=21*60;1827=21*87;2187=27*81;写一个程序,找出4位数的所有吸血鬼数字。等下我写完了,一起交流下》》》》》》

解决方案 »

  1.   


    public class TestNumber { public static void main(String[] args) {
    for (int i=1001; i<9999; i++) {
    String str = String.valueOf(i);
    String start = str.substring(0, 2);
    start = start.substring(1, 2) + start.substring(0, 1);
    String end = str.substring(2);
    int num1 = Integer.parseInt(start);
    int num2 = Integer.parseInt(end);
    int s = num1 * num2;
    if (num2 != 0 && s == i)
    System.out.println(num1 + " " + num2);
    }
    }
    }
      

  2.   

    public class Cao_feng { void func() {
    for (int i = 1000; i < 10000; i++) {
    String str = String.valueOf(i); // 把整数转化成字符串类型
    int k = Integer.parseInt(str); // 把字符串转化成为整形
    int i1 = Integer.parseInt(str.substring(0, 1)); // 获取第一个数字
    int i2 = Integer.parseInt(str.substring(1, 2)); // 获取第二个数字
    int i3 = Integer.parseInt(str.substring(2, 3)); // 获取第三个数字
    int i4 = Integer.parseInt(str.substring(3)); // 获取第四个数字 int j1 = i1 * 10 + i2; // 以下是一些组合,一共有12个
    int j2 = i1 * 10 + i3;
    int j3 = i1 * 10 + i4; int j4 = i2 * 10 + i1;
    int j5 = i2 * 10 + i3;
    int j6 = i2 * 10 + i4; int j7 = i3 * 10 + i1;
    int j8 = i3 * 10 + i2;
    int j9 = i3 * 10 + i4; int j10 = i4 * 10 + i1;
    int j11 = i4 * 10 + i2;
    int j12 = i4 * 10 + i3; if (j1 * j9 == k) // 以下是一些可能相等的情况,一个有12种
    System.out.println(k + "=" + j1 + "*" + j9);
    if (j1 * j12 == k)
    System.out.println(k + "=" + j1 + "*" + j12);
    if (j2 * j6 == k)
    System.out.println(k + "=" + j2 + "*" + j6);
    if (j2 * j11 == k)
    System.out.println(k + "=" + j2 + "*" + j11);
    if (j3 * j5 == k)
    System.out.println(k + "=" + j3 + "*" + j5);
    if (j3 * j8 == k)
    System.out.println(k + "=" + j3 + "*" + j8);
    if (j4 * j9 == k)
    System.out.println(k + "=" + j4 + "*" + j9);
    if (j4 * j12 == k)
    System.out.println(k + "=" + j4 + "*" + j12);
    if (j5 * j3 == k)
    System.out.println(k + "=" + j5 + "*" + j3);
    if (j5 * j10 == k)
    System.out.println(k + "=" + j5 + "*" + j10);
    if (j6 * j2 == k)
    System.out.println(k + "=" + j6 + "*" + j2);
    if (j6 * j7 == k)
    System.out.println(k + "=" + j6 + "*" + j7); }
    } public static void main(String args[]) { // 主类
    new Cao_feng().func();
    }}
    刚做完》》》》》
      

  3.   

    public class TestNumber {
    public static String bubSort(char[] arg1){
    for(int innerLoop1=0;innerLoop1<arg1.length;innerLoop1++){
    for(int innerLoop2=0;innerLoop2<arg1.length-innerLoop1-1;innerLoop2++){
    if(arg1[innerLoop2]<arg1[innerLoop2+1]){
    char tmpChar=arg1[innerLoop2];
    arg1[innerLoop2]=arg1[innerLoop2+1];
    arg1[innerLoop2+1]=tmpChar;
    }
    }
    }
    return new String(arg1);
    }
    public static void main(String[] args) {
    for(int multiplier=10;multiplier<=99;multiplier++){
    for(int multiplicand=10;multiplicand<=99;multiplicand++){
    int product=multiplier*multiplicand;
    if( String.valueOf(product).length()==4){
    String strProduce=String.valueOf(product);
    String strMults=String.valueOf(multiplier)+String.valueOf(multiplicand);
    char[] charProduct={strProduce.charAt(0),strProduce.charAt(1),strProduce.charAt(2),strProduce.charAt(3)};
    char[] charMults={strMults.charAt(0),strMults.charAt(1),strMults.charAt(2),strMults.charAt(3)};
    if(bubSort(charProduct).equals(bubSort(charMults)))
    System.out.println(strProduce+"="+multiplier+"*"+multiplicand);
    }
    }
    }
    }
    }1395=15*93
    1260=21*60
    1827=21*87
    2187=27*81
    1530=30*51
    1435=35*41
    1435=41*35
    1530=51*30
    1260=60*21
    6880=80*86
    2187=81*27
    6880=86*80
    1827=87*21
    1395=93*15
      

  4.   

      改成
    for(int multiplicand=multiplier;multiplicand<=99;multiplicand++)
      

  5.   


    package test.jm.test;import java.util.Calendar;
    import java.util.Locale;public class Asdf {
        public static void main(String[] args) {
         Calendar calendar = Calendar.getInstance(Locale.CHINA);
         calendar.add(Calendar.MONTH, -1);
         System.out.println(calendar);
            for (int i=1001; i<9999; i++) {
                String str = String.valueOf(i);
                String[] strSz = new String[4];
                for(int j = 0; j < str.length(); j++){
                 String str1 = str.substring(j, j + 1);
                 strSz[j] = str1;
                }
                String start = str.substring(0, 2);
                String end = str.substring(2);
                int num1 = Integer.parseInt(start);
                int num2 = Integer.parseInt(end);
                int s = num1 * num2;
                String newStr = Integer.toString(s);
                if(newStr.length() == 4){
                 if(newStr.endsWith("00")){
                 continue;
                 } else if(sfcz(strSz, newStr)){
                 System.out.println(num1 + " " + num2 + " = " + s);
                 }
                }
            }
        }
        
        private static boolean sfcz(String[] str, String newStr){
         for(int i = 0; i < str.length; i++){
         String stri = str[i];
         if(newStr.contains(stri)){
         newStr = newStr.replace(stri, "");
         } else {
         return false;
         }
         }
         return true;
        }
    }
      

  6.   

      public static void main(String[] args) {
            // TODO code application logic here
            for(int i = 10;i<100;i++)
            {
                for(int j = 10;j<100;j++)
                {
                    int tmp = i*j;
                    if(tmp < 1000){//如果小于1000,直接跳到i*j能大于1000的最小值
                        j = Goto(i);
                        continue;
                    }
                }
            }
        }    public static int Goto(int i){
            return 1000%i ==0 ? 1000/i:1000/i+1;
        }
    排除掉一些不必要的循环,其他地方都差不多,取字符串。
      

  7.   

    for (int i=10;i<=99;i++) {
    if (i/10 != i%10) {
    for (int j=i;j<=99;j++) {
    if (j/10 == j%10) {
    continue;
    }

    int t = i * j;

    if (t<1000 || t % 100 == 0) {
    continue;
    }

    String st = String.valueOf(t);

    if (st.contains(String.valueOf(i/10))
    &&st.contains(String.valueOf(i%10))
    &&st.contains(String.valueOf(j/10))
    &&st.contains(String.valueOf(j%10))
    )
    {
    String ij = new StringBuilder().append(i).append(j).toString();

    if (ij.contains(String.valueOf(st.charAt(0)))
    &&ij.contains(String.valueOf(st.charAt(1)))
    &&ij.contains(String.valueOf(st.charAt(2)))
    &&ij.contains(String.valueOf(st.charAt(3))))
    {
    System.out.println(i + " * " + j + " = " + t);
    }
    }
    }
    }
    }
    15 * 93 = 1395
    21 * 60 = 1260
    21 * 87 = 1827
    27 * 81 = 2187
    30 * 51 = 1530
    35 * 41 = 1435
    80 * 86 = 6880
    91 * 98 = 8918
      

  8.   

    楼主你也是看的java编程思想?
    一起交流啊!!
      

  9.   

    class Vampire 
    {    
      Vampire() 
      {    
        String[] ar_str1, ar_str2;    
        int sum = 0;    
        int from;    
        int to;    
        int i_val;    
        int count = 0;    
        // 双重循环穷举    
        for (int i = 10; i < 100; i++) 
        {    
          // j=i+1避免重复    
          from = Math.max(1000 / i, i + 1);    
          to = Math.min(10000 / i, 100);    
          for (int j = from; j < to; j++) 
          {    
             i_val = i * j;    
             
              if (i_val % 100 == 0 || (i_val - i - j) % 9 != 0) 
             {    
               continue;    
             }    
             count++;    
             ar_str1 = String.valueOf(i_val).split("");    
             ar_str2 = (String.valueOf(i) + String.valueOf(j)).split("");    
             Arrays.sort(ar_str1);    
             Arrays.sort(ar_str2);    
             if (Arrays.equals(ar_str1, ar_str2)) 
             {// 排序后比较,为真则找到一组    
                 sum++;    
               System.out.println("第" + sum + "组: " + i + "*" + j + "=" + i_val);    
             }    
           }    
         }    
         System.out.println("共找到" + sum + "组吸血鬼数");    
         System.out.println(count);    
       }    
    }
      

  10.   

    不必要用四位数找匹配二位数,用二位数相乘得四位数更好点。
    用四位数单循环需要循环9000次,而用两位数双循环相乘总循环4000多次
    列出双循环,可以从12开始循环,因为11*99还不到1000。
    还有听说吸血鬼数字符合条件 (vamnub-i-j)%9==0,这个可以提高很多效率。
    不知道字符串那个方法的可以再用双循环判断。for(int i=12;i<100;i++){
        for(int j=i;j<100;j++){
    //判断
        }
    }