如何将第一个数字(如果不是数字中最大的数字)与整数中的最大数字交换

时间:2015-01-10 07:28:20

标签: java algorithm

编辑:我无法跟踪整数中的第二大数字并将其与整数中的第一个数字交换以使整数最大...(例如,如果我的输入为{{1我无法与8交换3,因为我的代码将3与5交换并给出31548并再次运行其余部分然后交换为8并提供我不想要的51348。 ..)和对于最高位已经在最左边位置的数字序列,如果我的输入是877115755,则输出应忽略前三位数877并考虑较小的第四位1并交换下一次出现的7并输出877715155。

81345

输入为public static void main(String[] args) { int value = 621007349; String number = String.valueOf(value); int[] arr = new int[number.length()]; int length = (int) (Math.log10(value) + 1); for (int i = 0; i < length; i++) { System.out.println("Char at " + number.charAt(i)); arr[i] = number.charAt(i) - '0'; } int var = 0; for (int i = 0; i < arr.length; i++) { int temp = arr[i]; for (int j = i; j < arr.length-1; j++) { if (j < arr.length - 1) { if (arr[j + 1] > temp) { if (var < arr[j + 1]) { var = arr[j + 1]; System.out.println("Larg Val " + arr[j+1]); System.out.println("First no "+arr[i]); int tmp = arr[j+1]; arr[j+1] = arr[i]; arr[i] = tmp; } } } } } for (int j = 0; j < arr.length; j++) { System.out.println("Values in arry are " + arr[j]); } } ,输出应为621007349

请帮助我哪里出错了?

2 个答案:

答案 0 :(得分:0)

您可以尝试先获得max位置,然后进行简单的交换:

public static int get_max(int[] arr){
  int max_pos = 0;
  int max = Integer.MIN_VALUE;
  for (int i = 0; i < arr.length; i++) {
    if ( arr[i] > max) {
      max = arr[i];
      max_pos = i;
    }
  }
  return max_pos;
}

public static void swap(int[] arr, int st, int dt) {
  int temp = arr[st];
  arr[st] = arr[dt];
  arr[dt] = temp;
}

public static void main(String[] args) {

    int value = 621007349;

    String number = String.valueOf(value);

    int[] arr = new int[number.length()];

    int length = (int) (Math.log10(value) + 1);

    for (int i = 0; i < length; i++) {
        System.out.println("Char at " + number.charAt(i));
        arr[i] = number.charAt(i) - '0';
    }

    int pos_max = get_max(arr);
    swap(arr, 0, pos_max);

    for (int j = 0; j < arr.length; j++) {
        System.out.println("Values in arry are " + arr[j]);
    }
}

已更新:如果您想以非递增顺序替换最大数字:

public static int get_max(int[] arr, int stPosition){
  int max_pos = 0;
  int max = Integer.MIN_VALUE;
  for (int i = stPosition; i < arr.length; i++) {
    if ( arr[i] > max) {
      max = arr[i];
      max_pos = i;
    }
  }
  return max_pos;
}
// swap is same

// replace get_max and swap with
for (int i = 0; i < arr.length; i++) {
  int pos_max = get_max(arr, i); // gets max from i to arr.length
  if (pos_max == i)
    continue;
  swap(arr, i, pos_max);
}

答案 1 :(得分:-1)

编辑:添加一个while循环以替换第一个可能的数字,然后用最大数字替换:

我删除内部循环并稍微改变条件(也从循环内部删除掉到后循环),现在your code正常工作:

public static void main (String[] args) throws java.lang.Exception
{
    int value = 877115755;

    String number = String.valueOf(value);

    int[] arr = new int[number.length()];

    int length = (int) (Math.log10(value) + 1);
    for (int i = 0; i < length; i++) {
        arr[i] = number.charAt(i) - '0';
    }
    int var = arr[0];
    int index = 0;
    int startIndex = 0;
    boolean changed = false;

    while(startIndex<arr.length-1){
        var = arr[startIndex];
        for (int i = startIndex+1; i < arr.length; i++) {
            int temp = arr[i];
            if (temp > var) {
                var = temp;
                index = i;
                changed = true;
                System.out.println("Larg Val "+arr[i]);
            }
        }
        if(changed)
            break;
        startIndex++;
    }

    //this is the swap part
    int tmp = arr[index];
    arr[index] = arr[startIndex];
    arr[startIndex] = tmp;  

    for (int j = 0; j < arr.length; j++) {
        System.out.println("Values in arry are " + arr[j]);
    }
}