冒泡排序

无聊写点代码玩玩

public class Toys {

	public static void main(String[] args) {
		for (int i = 0; i < 10000; i++) {
			if (isResult(i)) {
				System.out.println(i);
			}
		}
	}

	// 是否满足目标
	private static boolean isResult(int source) {
		int[] b = toArray(source);
		b = sort(b);
		int max = maxNumber(b);
		int min = minNumber(b);
		return max - min == source ? true : false;
	}

	// 将一个整数转为整型数组
	private static int[] toArray(int source) {
		int temp[] = new int[10];
		int i = 0;
		while (source > 0) {
			temp[i++] = source % 10;
			source /= 10;
		}
		int result[] = new int[i];
		for (int k = 0; k < i; k++) {
			result[k] = temp[k];
		}
		return result;
	}

	// 冒泡排序
	private static int[] sort(int[] source) {
		int result[] = source;
		for (int j = result.length - 1; j > 0; j--) {
			for (int k = 0; k < j; k++) {
				if (result[k] < result[k + 1]) {
					result[k] ^= result[k + 1];
					result[k + 1] ^= result[k];
					result[k] ^= result[k + 1];
				}
			}
		}
		return result;
	}

	// 组成一个整数
	private static int maxNumber(int[] source) {
		int max = 0;
		for (int i = 0; i < source.length; i++) {
			max *= 10;
			max += source[i];
		}
		return max;
	}

	// 组成一个整数
	private static int minNumber(int[] source) {
		int min = 0;
		for (int i = source.length - 1; i >= 0; i--) {
			min *= 10;
			min += source[i];
		}
		return min;
	}

}
有没有bug还没有测,懒人一个!大笑大笑

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值