Java编程题,求解.

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

例如:4321
[1]:4321-1234=3087
[2]:8730-378=8352
[3]:8532-2358=6174
经过3次运算,结果为6174。

接口说明
原型:
int VerifyKabulekeTheorem(int m);
输入参数:
int m:四位整数且各位数字不同(输入已保证)
返回值:
0:整数m不是卡布列克数
>0: 结果为6174需要运算的次数
可追加分数,要详细步骤.

要看就是C的算法题,最讨厌非JAVA的给JAVA乱支招

我这个效率可能有点低,你看看吧

import java.util.Set;
import java.util.TreeSet;

public class $ {

    private static int count = 0;

    public static void main(String[] args) {
        System.out.println(VerifyKabulekeTheorem(1223));
    }

    private static int VerifyKabulekeTheorem(int num) {

        String str = String.valueOf(num);

        // 校验,不足4位,返回0
        if (str.length() < 4) {
            return 0;
        }

        // 校验有重复,返回0
        Set<Integer> data = new TreeSet<Integer>();
        int size = 0;

        for (int i = 0; i < str.length(); i++) {
            data.add(toInt(str.substring(i, i + 1)));

            if (size + 1 > data.size()) {
                return 0;
            }
            size = data.size();
        }

        count++;

        // 获取最大最小值
        StringBuffer tmp = new StringBuffer();
        for (Integer integer : data) {
            tmp.append(integer);
        }

        int min = toInt(String.valueOf(tmp));
        int max = toInt(String.valueOf(tmp.reverse()));

        int n = max - min;

        if (n == 6174) {
            return count;
        }

        VerifyKabulekeTheorem(n);

        return count;
    }

    private static int toInt(String str) {
        return Integer.parseInt(str);
    }
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-09-23

你好!


不考虑性能的写了段代码······

import java.util.Arrays;

public class Test{
    //计算次数
    static int count = 0;
    //计算结果
    static int result = -1;

    public static void main(String[] args){
        int m = 5556;
        int cnt = VerifyKabulekeTheorem(m);
        System.out.format( "%d 得到6174卡布列克常数的计算步骤为 %d 步!%n",m,cnt);
    }

    static int VerifyKabulekeTheorem(int m){
        //临时正序数组,即最小4位数
        int[] tmpAsc  = new int[4];
        //临时倒序数组,即最大4位数
        int[] tmpDesc  = new int[4];
        //由m计算出的数组
        int[] arr  = new int[4];
            
        //计算千位
        arr[0] = m / 1000;
        //计算百位
        arr[1] = (m % 1000) / 100;
        //计算十位
        arr[2] = (m % 100) / 10;
        //计算个位
        arr[3] = m % 10;

        //将由m计算得出的数组赋值给正序数组(此时未排序)
        tmpAsc = arr;
        //从小到大排序
        Arrays.sort(tmpAsc);
        //从大到小排序
        for(int j=arr.length;j>0;j--){
            tmpDesc[arr.length-j] = tmpAsc[j-1];
        }
        //分别得到最大4位数和最小4位数字符串
        String maxStr = Arrays.toString(tmpDesc).replace("[","").replace("]","").replace(",","").replace(" ","");
        String minStr = Arrays.toString(tmpAsc).replace("[","").replace("]","").replace(",","").replace(" ","");
        //将最大4位数和最小4位数字符串转为整型数字
        int maxNum = Integer.parseInt(maxStr);
        int minNum = Integer.parseInt(minStr);

        //结果不为6174将结果传入自身方法,进入递归调用
        while(result!=6174){
            result = maxNum - minNum;
            count ++;
            VerifyKabulekeTheorem(result);
        }
            
        return count;    
    }
}

本回答被提问者采纳
第2个回答  2013-09-23
package com.mapabc.test;

public class SeleSort {
public static void main(String[] args){
int a = 1234;
int b = 0;
int n = 1;
while (a != b) {
System.out.println(a + "***" + b);
b = a;
a = myfun(b);
n++;
}
System.out.println("共需要次数为:" + n);
}

public static int myfun(int n) {
//int a[] = new int[args.length];
int[] a = {n/1000, n%1000/100, n%100/10, n%10};

System.out.println("--------------------");
print(a);
seleSort(a);
int x = (a[0]*999+a[1]*90-a[2]*90-a[3]*999);
return x>0? x : -x;
//print(a);
}
// private static void seleSort(int[] a){
// for (int i=0;i<a.length;i++){
// for (int j=i+1;j<a.length;j++){
// if(a[j]<a[i]){
// int temp = a[i];
// a[i] = a[j];
// a[j] = temp;
// }
// }
// print(a);
// }
// }
private static void seleSort(int[] a){
for (int i=0;i<a.length;i++){
int tmpx = i,min = a[tmpx];
for (int j=i+1;j<a.length;j++){
if(min>a[j]) {
tmpx = j;
min = a[tmpx];
}
}
a[tmpx] = a[i];
a[i] = min;
print(a);
}
}
private static void print(int[] a){
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
System.out.println();
}
}
第3个回答  2013-09-23
觉得爆你菊花 黄姐和v肺结核积分飞v飞机飞机合法房间