给定一个集合A=[0,1,3,8](该集合中的元素都是在0,9之间的数字,但未必全部包含),指定任意一个正整数K,请用A中的元素组成一个大于K的最小正整数。 
* 比如,A=[1,0] K=21 那么输出结构应该为100 有了思路,但写了一晚上没写出来

解决方案 »

  1.   

    还真是有点难度,搞了一个多小时.这个题目玩了一个数字游戏,我们只需要找出最大的一位,后面补上最小的数字,就得到答案了.public class TTT { /**
     * @param args
     */
    public static void main(String[] args) {
    int[] list = { 0, 1, 3, 4, 7, 8 };
    // int[] list = { 0, 1,8 };
    // 先对list排序,这里默认排好序,并且去掉重复项 String value = "138"; System.out.println("value is 1000 result is " + haha1("931", list));
    System.out.println("value is 1000  result is " + haha1("999", list));
    System.out.println("value is 1000  result is " + haha1("888", list));
    System.out.println("value is 1000  result is " + haha1("891", list));
    System.out.println("value is 300 result is " + haha1("234", list));
    System.out.println("value is 330 result is " + haha1("324", list));
    System.out.println("value is 3 result is " + haha1("1", list));
    System.out.println("value is 1 result is " + haha1("0", list));
    System.out.println("value is 10 result is " + haha1("9", list));
    System.out.println("value is 100 result is " + haha1("90", list));
    System.out.println("value is 137 result is " + haha1("135", list));
    System.out.println("value is 140 result is " + haha1("138", list));
    System.out.println("value is 138 result is " + haha1("137", list));
    } public static String haha1(String value, int[] list) {
    String result = "";
    String preMax = ""; for (int i = 0; i < value.length(); i++) {
    int tempValue = Integer.parseInt(value.substring(i, i + 1));
    boolean hasBreak = false;
    for (int j = 0; j < list.length; j++) {
    if (list[j] == tempValue) {
    hasBreak = true;
    if (j < list.length - 1) {
    preMax = value.substring(0, i) + list[j + 1];
    } else {
    if (preMax.equals("")) {
    if (list[0] == 0) {
    for (int aa = i; aa >= 0; aa--) {
    preMax = preMax + list[0];
    }
    preMax = list[1] + preMax;
    } else {
    for (int aa = i; aa >= 0; aa--) {
    preMax = preMax + list[0];
    }
    }
    } else {
    preMax = preMax + list[0];
    }
    }
    break;
    } else if (list[j] > tempValue) {
    if (i == 0) {
    result = String.valueOf(list[j]);
    } else {
    result = value.substring(0, i) + list[j];
    }
    return replace(result, String.valueOf(list[0]), value.length()
    - result.length());
    }
    }
    if (preMax.equals("")) {
    if (list[0] == 0) {
    result = list[1] + "" + list[0];
    return replace(result, String.valueOf(list[0]), value.length() - 1);
    } else {
    result = list[0] + "" + list[0];
    return replace(result, String.valueOf(list[0]), value.length() - 1);
    }
    }
    if (!hasBreak) {
    for (int k = i; k < value.length(); k++) {
    result = result + list[0];
    }
    return preMax + result;
    }
    } return preMax;
    } // 将除第一位之外的全都置为最小值
    public static String replace(String value, String key, int num) {
    for (int i = 0; i < num; i++) {
    value = value + key;
    }
    return value;
    }
    }
      

  2.   

    public static int isContain(int[] A,int K)
    {
    int returnValue=-1;
    int temp=1;


    while(K++>0)
    {
    int tempCondition=0;
    temp=K;
    while(temp>0)
    {
    for(int i=0;i<A.length;i++)
    {
    if(A[i]==temp%10)
    {
    tempCondition++;
    break;
    }
    }
    temp/=10;
    }

    if(tempCondition==String.valueOf(K).length())
    {
    returnValue=K;
    break;
    }
    }
    return returnValue;
    }