日期:2014-05-20 浏览次数:20999 次
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class NumberTest {
public static void main(String[] args) throws Exception {
StringBuilder temp = new StringBuilder();
Set<String> set = new HashSet<String>();
int[] a = { 11111, 34256 };
for (int i : a) {
List<Integer> list = getCircle(i);
if (list.size() > 0) {
temp.delete(0, temp.length());
temp.append('[');
for (Integer num : list)
temp.append(num + ",");
temp.append(8 + "]");
set.add(temp.toString());
}
}
for (String str : set)
System.out.println(str);
}
static List<Integer> getCircle(int num) throws Exception {
List<Integer> circle = new ArrayList<Integer>();
while (true) {
num = getNextNumber(num);
if (circle.contains(num)) {
while (circle.indexOf(num) > 0)
circle.remove(0);
return (circle);
}
circle.add(num);
}
}
static int getNextNumber(int num) throws Exception {
List<Integer> temp = new ArrayList<Integer>();
Object[] nums;
int big = 0, small = 0;
if (num < 0)
throw new Exception("参数错误");
while (num > 0) {
temp.add(num % 10);
num /= 10;
}
nums = temp.toArray();
Arrays.sort(nums);
for (Object n : nums)
small = small * 10 + (Integer) n;
for (int i = nums.length - 1; i >= 0; i--)
big = big * 10 + (Integer) nums[i];
return (big - small);
}
}