1

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

    public static int maximum(int max){

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

8 回答 8

3

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

于 2013-11-14T12:18:28.013 回答
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);
}
于 2013-11-14T12:28:00.790 回答
0

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

   if (times[i] > maxValue) {
        secondhighest  = maxValue;
        maxValue = times[i];
    } else if (times[i] > secondhighest) {
        secondhighest  = times[i];
    }
于 2013-11-14T12:30:37.620 回答
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);
于 2013-11-14T12:46:36.397 回答
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};
    }
于 2013-11-14T12:26:36.047 回答
0

对数字的数字列表进行排序并获得第一个和第二个最大的数字将为您提供最佳的O(n * log n)时间复杂度(假设您将使用Quick Sort)。
如果您将使用另一种方法,您可以获得更好的性能:分区(重新排序)您的数组(如快速排序),因此您将拥有一个将数组分为两部分的枢轴值:小于枢轴的那些在左边部分(左子数组),较大的在右边部分(右子数组)。检查枢轴的索引:

  • 如果它等于数字数组的大小 - 2,则它是第二大元素(第一个大元素在它旁边,在右侧子数组中);
  • 如果枢轴的索引小于数字数组的大小减2,则对右子数组重复分区;
  • 如果枢轴的索引大于数字数组的大小减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方法对数组进行重新排序并返回中位数的索引,您可以自己弄清楚如何编写它的实现,或者通过网络搜索。

于 2013-11-14T13:05:29.180 回答
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;

}
于 2018-01-02T07:36:26.447 回答
-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);
    }
于 2015-12-02T10:38:41.613 回答