验证卡布列克运算。任意一个四位数,只要它们各个位上的数字是不全相同的,就有这样的规律:
1)将组成该四位数的四个数字由大到小排列,形成由这四个数字构成的最大的四位数;
2)将组成该四位数的四个数字由小到大排列,形成由这四个数字构成的最小的四位数(如果四个数中含有0,则得到的数不足四位);
3)求两个数的差,得到一个新的四位数(高位零保留)。
重复以上过程,最后得到的结果是6174,这个数被称为卡布列克数。
谢谢

解决方案 »

  1.   

    不知道这样合不合你的意:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;public class newTest {
    static int num;
    static int t;
    static int count = 0; public static void main(String[] args) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(
    System.in));
    System.out.print("任意输入一个四位数:");
    String str = reader.readLine(); if (str.length() != 4) {
    System.out.println("不足四位,现在赋值t=9876。");
    t = 9876;
    } else {
    try {
    t = Integer.parseInt(str); // 判断是不是有相同的数字
    L: for (int i = 0; i < 3; i++) {
    for (int j = i + 1; j < 4; j++) {
    if (str.charAt(i) == str.charAt(j)) {
    System.out.println("有相同数字,现在赋值t=9876。");
    t = 9876;
    break L;
    }
    }
    }
    } catch (NumberFormatException exp) {
    System.out.println("输入出错,现在赋值t=9876。");
    t = 9876;
    }
    } while (num != 6174) {
    System.out.printf("第%d次了。\n", ++count);
    String s = Integer.toString(t);
    char cNum[] = s.toCharArray();
    for (int i = 0; i < 3; i++) {
    for (int j = i + 1; j < 4; j++) {
    if (cNum[i] < cNum[j]) {
    char x = cNum[i];
    cNum[i] = cNum[j];
    cNum[j] = x;
    }
    }
    } s = new String(cNum);
    num = Integer.parseInt(s);
    StringBuffer sb = new StringBuffer(s);
    sb.reverse();
    s = new String(sb);
    t = Integer.parseInt(s); num -= t;
    t = num;
    } System.out.println("Yeah!搞定了。");
    }
    }
      

  2.   

    import java.util.Arrays;public class Kblk { /**
     * @param value
     *            被操作的整数
     * @param sortWay
     *            排序的方式 0:升序 other:降序
     * @return 排序后的整数
     */
    public int sort(int value, int sortWay) {
    int i = 0;
    int[] arr = new int[4];
    int returnValue = 0; while (value > 0) {
    arr[i] = value % 10;
    value = value / 10;
    i++;
    }
    // 按升序排列
    if (sortWay == 0) {
    Arrays.sort(arr);
    }
    //按降序排列
    else {
    for (int iJ = 0; iJ < arr.length - 1; iJ++) {
    for (int kJ = iJ + 1; kJ < arr.length; kJ++) {
    if (arr[kJ] >= arr[iJ]) {
    int tempJ = arr[kJ];
    arr[kJ] = arr[iJ];
    arr[iJ] = tempJ;
    }
    }
    }
    }
    // 重新变为整数
    for (int j = 0; j < arr.length; j++) {
    int k = arr[j];
    returnValue += Math.pow(10, 3-j) * k;
    }
    return returnValue;
    } public int sort(int value) {
    return sort(value,0);
    }

    //验证卡布列克运算
    public int kblk(int value)
    {
    while(value != (sort(value,1)-sort(value)))
    {
    value = sort(value,1)-sort(value);
    }
    return value;
    }

    public static void main(String[] args) {
    System.out.println(new Kblk().kblk(2641));
    }
    }
      

  3.   


    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;   
      }   
        
      }   
      

  4.   


    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;   
      }   
        
      }   
      

  5.   

    感谢 jeff_jian,kingssql,zwpjava.
      

  6.   

    public class Test{
    static int a,b,c,d,h,i,j,k;
    public static int[] sort(int n){
    a=n/1000;
    b=(n/100)%10;
    d=n%10;
    c=(n%100)/10;
    h=(a>=b?a:b)>=(c>=d?c:d)?(a>=b?a:b):(c>=d?c:d);
    i=(a>=b?a:b)>=(c>=d?c:d)?(c>=d?c:d):(a>=b?a:b);
    j=(a>=b?b:a)>=(c>=d?d:c)?(a>=b?b:a):(c>=d?d:c);
    k=(a>=b?b:a)>=(c>=d?d:c)?(c>=d?d:c):(a>=b?b:a);
    return new int[]{h*1000+i*100+j*10+k,k*1000+j*100+i*10+h};
    }

    public static void main(String...strings ){
    int[] aa;
    for(int n=1000;n<9998;n++){
    aa=sort(n);
    while(aa[0]-aa[1]!=0){
    if(aa[0]-aa[1]==6174){
    System.out.println(n);
    break;
    }else{
    aa=sort(aa[0]-aa[1]);
    }
    }
    }
    }
    }