请教各位高手,碰到一难提,如何用java实现数学黑洞6174的问题:
1、 数学黑洞6174
    已知:一个任意的四位正整数。将数字重新组合成一个最大的数和最小的数相减,重复这个过程,最多七步,必得6174。即:7641-1467=6174。将永远出不来。
    求证:所有四位数数字(全相同的除外),均能得到6174。输出掉进黑洞的步数。

解决方案 »

  1.   

    int n = new Random().nextInt(9999);
        System.out.println("n="+n);
        for (int times = 0; times < 7; times++) {
          int[] data = new int[4];
          String temps = String.valueOf(n);
          for (int datai = 0, si = temps.length() - 1; si >= 0; si--, datai++) {
            data[datai] = Character.digit(temps.charAt(si),10);
          }
          Arrays.sort(data);
          int big=0;
          int small=0;
          small=data[0]*1000+data[1]*100+data[2]*10+data[3];
          big=data[3]*1000+data[2]*100+data[1]*10+data[0];
          n=big-small;
          System.out.println(times+":\t"+n);
        }这个题有意思
      

  2.   

    import java.io.*;
    import java.util.Arrays;public class MathHole
    { /**
     * @param args
     * @author goodboye
     */
    public MathHole()
    {

    } public static void main(String[] args)
    {
    BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
    String input = null;

    try
    {
    input = bin.readLine();
    String result = "";
    String temp = input;

    int count = 1;

    while(true)
    {
    System.out.println("第"+count+"次!");
    System.out.println(max(temp));
    System.out.println(min(temp));
    temp = new Integer(Integer.parseInt(max(temp))-Integer.parseInt(min(temp))).toString();
    System.out.println(temp);
    result = temp;
    if (result.equals("6174")) break;
    count++;
    }


    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }

    public static String max(String temp)
    {
    char chararray [] = new char[4];
    chararray = temp.toCharArray();
    Arrays.sort(chararray);
    temp = new String(chararray);
    String a = (new StringBuffer(temp)).reverse().toString();
    if(a.length()==3)
    { return a+"0";
    }
    else if(a.length()==2)
    {
    return a+"00";
    }
    else if(a.length()==1){
    return a+"000";
    }
    return a;

    }
    public static String min(String temp)
    {
    char chararray [] = new char[4];
    chararray = temp.toCharArray();
    Arrays.sort(chararray);
    temp = new String(chararray);

    return temp;
    }}