找到一个数字中的最高和第二高位数

时间:2013-11-14 12:13:54

标签: java

在找到数字中的最高位后,如何仅使用循环和if语句找到相同数字中的第二高位?

    public static int maximum(int max){

while(num != 0){
            int rightDigit = num % 10;
            num /= 10;
            if(rightDigit > max)
                rightDigit = max;
        }
        return max;
        }

10 个答案:

答案 0 :(得分:3)

使用List存储所有数字和sort,这样您就可以根据需要访问最高,第二高到最低的数字。要对List使用Collections.sort(List list)

进行排序

答案 1 :(得分:0)

int largest = 0;
int secondLargest = 0;

public static void minAndMax(int n){

    int num = n;
    int currentNum = 0;

    while(num % 10 != 0){
        currentNum = num % 10;

        if(currentNum > secondLargest) {
            secondLargest = currentNum;
        }
        if(secondLargest > largest) {
            largest = secondLargest;  
        }
        num /= 10; 
    }
}

答案 2 :(得分:0)

这是你想要的吗?数组中的第一个元素是最大的,第二个元素是第二大元素。如果没有这样的元素,则返回-1。

public static void main(String[] args) {
    int[] tab = maximum(12);
    System.out.println("Largest digit: " + tab[0]);
    System.out.println("Second largest digit: " + tab[1]);
}
public static int[] maximum(int max){
    int num = max;
    int largest = -1;
    int secondLargest = -1;
    while(num != 0){
        int rightDigit = num % 10;
        num /= 10;

        if(rightDigit > largest) {
            secondLargest = Math.max(secondLargest, largest);
            largest = rightDigit;

        } else if(rightDigit > secondLargest)
            secondLargest = rightDigit;
    }
    return new int[]{largest,secondLargest};
    }

答案 3 :(得分:0)

public int secondMax(int number){
  List<Integer> list= new ArrayList<Integer>();
  while (number > 0) {
      list.add( number % 10 );
      number = number / 10;
  }

  Collections.sort(list);
  int size= list.size();   
  return list.get(size - 2);
}

答案 4 :(得分:0)

假设maxValue是最高的,您可以轻松识别第二高

   if (times[i] > maxValue) {
        secondhighest  = maxValue;
        maxValue = times[i];
    } else if (times[i] > secondhighest) {
        secondhighest  = times[i];
    }

答案 5 :(得分:0)

int num = 1395248, n, i, n2;
for (n2 = i = n = 0; num > 0; i = num % 10, n2 = n < i ? n : n2, n = n < i ? i : n, num /= 10);
System.out.println(n);
System.out.println(n2);

答案 6 :(得分:0)

对数字列表进行排序并获得第一和第二大数字将最大限度地提高O(n * log n)时间复杂度(假设您将使用Quick Sort)。
如果你将使用另一种方法,你可以获得更好的性能:分区(重新排序)你的数组(如快速排序),所以你将有一个数据库值,它将你的数组分成两部分:那些小于数据透视的数据是左侧部分(左侧子阵列),较大部分位于右侧部分(右侧子阵列)。检查数据透视的索引:

  • 如果它等于数字数组减去2的大小,那么它是第二大元素(第一个最大元素就在它旁边,在右子数组中);
  • 如果pivot的索引小于数字数组减去2的大小,则重复右子数组的分区;
  • 如果pivot的索引大于数字数组减去2的大小,则重复左子数组的分区;

在某些时候,您的数据透视图将是数组末尾的第二个元素,这意味着它是第二大元素,最大数字位于数组末尾(因为您获得数据的方式) )。时间复杂度将比快速排序更好,因为在每个分区之后,您只分区一个子数组,而不是两个子数组。

您可以扩展此方法,不仅可以获得第1和第2个最大数字,而且可以获得第k个(任意最高)数字,而且不仅可以是最大数字,还可以是最小数字。

查看我几天前写过的代码:

public Long selectKthElement(int left, int right, int k, Type type) {
    int med = partitionIt(left, right);

    if ((type.equals(Type.Smallest) && med == k - 1) || (type.equals(Type.Largest) && med == nElems - k)) {
        return theArray[med];
    } else if (type.equals(Type.Smallest) && med > k || type.equals(Type.Largest) && med > nElems - k) {
        return selectKthElement(left, med - 1, k, type);
    } else if (type.equals(Type.Smallest) && med < k || type.equals(Type.Largest) && med < nElems - k){
        return selectKthElement(med + 1, right, k, type);
    } else {
        // impossible case, but the source code won't compile w/o the else
        return null;
    }
}

这里theArray是一个数字数字数组,partitionIt方法重新排序数组并返回中位数索引,你可以弄清楚如何自己编写实现,或搜索网络。

答案 7 :(得分:0)

你应该这样做。

保持两个整数变量,最高和第二高 尽你所能,和你的第一个if语句一样,就像你已经展示过的代码一样 添加另一个if语句,检查secondhighest是否大于数字,而secondhighest小于最高

希望这会有所帮助。

答案 8 :(得分:0)

static void Main(string[] args)
{




    int max = 0, temp = 0, secondMax = 0, number = 0;

    number = 6541891;
    while (number != 0)
    {
        temp = number % 10;

        if (max == 0)
        {

            max = temp;
            secondMax = temp;

        }
        else if (temp > max)
        {
            int lastmax = max;

            max = temp;

            if (lastmax > secondMax)
            {
                secondMax = lastmax;
            }


        }

        if ((temp > secondMax && temp < max) || secondMax >= max)
        {
            secondMax = temp;
        }

        number = number / 10;
    }

    int Result = secondMax;

}

答案 9 :(得分:-1)

    public static int nthHighest(int[] arr, int n) {
         List<Integer> lst = Arrays.asList(ArrayUtils.toObject(arr)); //use apache commons library
         Collections.sort(lst);
         return lst.get(arr.length-n);
    }
相关问题