我很难理解该方法背后的逻辑,以找到数组中第二高的数字。使用的方法是找到数组中的最高值但小于先前的最高值(已经找到)。我仍然无法弄明白的是为什么|| highest_score == second_highest
是必要的。例如,我输入了三个数字:98,56,3。没有它,最高和第二高都是98.请解释。
int second highest = score[0];
if (score[i] > second_highest && score[i] < highest_score || highest_score == second_highest)
second_highest = score[i];
答案 0 :(得分:31)
我不相信你做的事情可以解决问题;我认为这掩盖了你的逻辑中的另一个问题。要找到第二高的实际上非常简单:
static int secondHighest(int... nums) {
int high1 = Integer.MIN_VALUE;
int high2 = Integer.MIN_VALUE;
for (int num : nums) {
if (num > high1) {
high2 = high1;
high1 = num;
} else if (num > high2) {
high2 = num;
}
}
return high2;
}
这是O(N)
一次通过。如果您想接受关联,请更改为if (num >= high1)
,但实际上,如果数组中至少有2个元素,它将返回Integer.MIN_VALUE
。如果数组只包含相同的数字,它也将返回Integer.MIN_VALUE
。
答案 1 :(得分:11)
// Initialize these to the smallest value possible
int highest = Integer.MIN_VALUE;
int secondHighest = Integer.MIN_VALUE;
// Loop over the array
for (int i = 0; i < array.Length; i++) {
// If we've found a new highest number...
if (array[i] > highest) {
// ...shift the current highest number to second highest
secondHighest = highest;
// ...and set the new highest.
highest = array[i];
} else if (array[i] > secondHighest)
// Just replace the second highest
secondHighest = array[i];
}
}
// After exiting the loop, secondHighest now represents the second
// largest value in the array
答案 2 :(得分:5)
如果second_highest最初设置为的第一个元素已经是最高元素,则在找到下一个元素时应将其重新分配给新元素。也就是说,它被初始化为98,应该设置为56.但是,56不高于98,所以除非你做检查,否则它不会被设置。
如果最高的数字出现两次,这将导致第二个值,而不是您排序数组时会找到的第二个元素。
答案 3 :(得分:2)
如果有两个相同的最大数字,我看到的答案不会起作用,如下例所示。
int[] randomIntegers = { 1, 5, 4, 2, 8, 1, 8, 9,9 };
SortedSet<Integer> set = new TreeSet<Integer>();
for (int i: randomIntegers) {
set.add(i);
}
// Remove the maximum value; print the largest remaining item
set.remove(set.last());
System.out.println(set.last());
我已将它从Set中删除,而不是从数组中删除
答案 4 :(得分:1)
public static int secondLargest(int[] input) {
int largest,secondLargest;
if(input[0] > input[1]) {
largest = input[0];
secondLargest = input[1];
}
else {
largest = input[1];
secondLargest = input[0];
}
for(int i = 2; i < input.length; i++) {
if((input[i] <= largest) && input[i] > secondLargest) {
secondLargest = input[i];
}
if(input[i] > largest) {
secondLargest = largest;
largest = input[i];
}
}
return secondLargest;
}
答案 5 :(得分:1)
我的想法是你假设阵列的第一个和第二个成员是你的第一个最大和第二个最大值。然后你获取一个数组的每个新成员,并将其与第二个最大值进行比较。不要忘记将第二个最大值与第一个最大值进行比较。如果它更大,只需交换它们。
public static int getMax22(int[] arr){
int max1 = arr[0];
int max2 = arr[1];
for (int i = 2; i < arr.length; i++){
if (arr[i] > max2)
{
max2 = arr[i];
}
if (max2 > max1)
{
int temp = max1;
max1 = max2;
max2 = temp;
}
}
return max2;
}
答案 6 :(得分:1)
public class SecondLargestNumberInArray
{
public static void main(String[] args)
{
int arr[] = {99, 76, 47, 85, 929, 52, 48, 36, 66, 81,9};
int largest = arr[0];
int secondLargest = arr[0];
System.out.println("The given array is:" );
boolean find=false;
boolean flag=true;
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i]+"\t");
}
System.out.println("");
while(flag)
{
for (int i = 0; i < arr.length; i++)
{
if (arr[i] > largest)
{
find=true;
secondLargest = largest;
largest = arr[i];
}
else if (arr[i] > secondLargest)
{
find=true;
secondLargest = arr[i];
}
}
if(find)
{
System.out.println("\nSecond largest number is:" + secondLargest);
flag=false;
}else
{
largest = arr[1];
secondLargest = arr[1];
}
}
}
}
Output is
The given array is:
99 76 47 85 929 52 48 36 66 81 9
Second largest number is: -> 99
答案 7 :(得分:0)
O(n / 2)中的第二大
public class SecMaxNum {
// second Largest number with O(n/2)
/**
* @author Rohan Kamat
* @Date Feb 04, 2016
*/
public static void main(String[] args) {
int[] input = { 1, 5, 10, 11, 11, 4, 2, 8, 1, 8, 9, 8 };
int large = 0, second = 0;
for (int i = 0; i < input.length - 1; i = i + 2) {
// System.out.println(i);
int fist = input[i];
int sec = input[i + 1];
if (sec >= fist) {
int temp = fist;
fist = sec;
sec = temp;
}
if (fist >= second) {
if (fist >= large) {
large = fist;
} else {
second = fist;
}
}
if (sec >= second) {
if (sec >= large) {
large = sec;
} else {
second = sec;
}
}
}
}
}
答案 8 :(得分:0)
想法是取两个变量 firstmax fm 和 secondmax sm 并遍历数组。
def print2largest(self,A,N):
#code here
if all(A[x]==A[x-1] for x in range(1,N)):
return -1
sm=0
fs=0
for i in range(N):
if A[i]>fs:
sm=fs
fs=A[i]
elif A[i]>sm and A[i]!=fs:
sm=A[i]
return sm
答案 9 :(得分:0)
问题: 问题是获得第二大数组元素。
观察: 第二大数字定义为从数组中的最大元素中减去时具有最小差异的数字。
解决方案: 这是一个两通解决方案。第一步是找到最大数量。第二遍是找到与其他数组元素相比与最大元素具有最小差异的元素。示例:在数组[2,3,6,5,5]中,最大值= 6,第二个最大值= 5,因为它与最大元素的最小差异为6 - 5 = 1,第二大的解是= 5
function printSecondMax(myArray) {
var x, max = myArray[0];
// Find maximum element
for(x in myArray){
if(max < myArray[x]){
max = myArray[x];
}
}
var secondMax = myArray[0], diff = max - secondMax;
// Find second max, an element that has min diff with the max
for(x in myArray){
if(diff != 0 && (max - myArray[x]) != 0 && diff > (max - myArray[x])){
secondMax = myArray[x];
diff = max - secondMax;
}
}
console.log(secondMax);
}
复杂性:O(n),这是最简单的方法。
为了更有效地查找最大元素,可以查看max heap,对max-heapify的调用将花费O(log n)时间来查找max,然后弹出top元素给出最大值。要获得第二个最大值,请在弹出顶部后继续弹出,然后继续弹出,直到得到小于最大值的数字。这将是第二个最大值。该解决方案具有O(n log n)复杂度。
答案 10 :(得分:0)
请尝试这一个:使用此方法,您可以在数组甚至数组中包含随机数的第二大数字。如果最大数字是数组的第一个索引,则第一个循环用于解决问题。
public class secondLargestnum {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array = new int[6];
array[0] = 10;
array[1] = 80;
array[2] = 5;
array[3] = 6;
array[4] = 50;
array[5] = 60;
int tem = 0;
for (int i = 0; i < array.length; i++) {
if (array[0]>array[i]) {
tem = array[0];
array[0] = array[array.length-1];
array[array.length-1] = tem;
}
}
Integer largest = array[0];
Integer second_largest = array[0];
for (int i = 0; i < array.length; i++) {
if (largest<array[i]) {
second_large = largest;
largest = array[i];
}
else if (second_large<array[i]) {
second_large = array[i];
}
}
System.out.println("largest number "+largest+" and second largest number "+second_largest);
}
}
答案 11 :(得分:0)
公共类SecondHighInIntArray {
public static void main(String[] args) {
int[] intArray=new int[]{2,2,1};
//{2,2,1,12,3,7,9,-1,-5,7};
int secHigh=findSecHigh(intArray);
System.out.println(secHigh);
}
private static int findSecHigh(int[] intArray) {
int highest=Integer.MIN_VALUE;
int sechighest=Integer.MIN_VALUE;
int len=intArray.length;
for(int i=0;i<len;i++)
{
if(intArray[i]>highest)
{
sechighest=highest;
highest=intArray[i];
continue;
}
if(intArray[i]<highest && intArray[i]>sechighest)
{
sechighest=intArray[i];
continue;
}
}
return sechighest;
}
}
答案 12 :(得分:0)
使用以下功能
`
public static int secHigh(int arr[]){
int firstHigh = 0,secHigh = 0;
for(int x: arr){
if(x > firstHigh){
secHigh = firstHigh;
firstHigh = x;
}else if(x > secHigh){
secHigh = x;
}
}
return secHigh;
}
函数调用
int secondHigh = secHigh(arr);
答案 13 :(得分:0)
import java.util.Scanner;
public class SecondLargest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter size of array : ");
int n = sc.nextInt();
int ar[] = new int[n];
for(int i=0;i<n;i++)
{
System.out.print("Enter value for array : ");
ar[i] = sc.nextInt();
}
int m=ar[0],m2=ar[0];
for(int i=0;i<n;i++)
{
if(ar[i]>m)
m=ar[i];
}
for(int i=0;i<n;i++)
{
if(ar[i]>m2 && ar[i]<m)
m2=ar[i];
}
System.out.println("Second largest : "+m2);
sc.close();
}
}
答案 14 :(得分:0)
public void findMax(int a[]) {
int large = Integer.MIN_VALUE;
int secondLarge = Integer.MIN_VALUE;
for (int i = 0; i < a.length; i++) {
if (large < a[i]) {
secondLarge = large;
large = a[i];
} else if (a[i] > secondLarge) {
if (a[i] != large) {
secondLarge = a[i];
}
}
}
System.out.println("Large number " + large + " Second Large number " + secondLarge);
}
上面的代码已经使用具有重复条目,负值的整数数组进行了测试。最大数量和第二大数量一次性回收。如果数组只包含多个相同数字的副本,如{8,8,8,8}或只有一个数字,则此代码才会失败。
答案 15 :(得分:0)
public class SecondLargestNumber
{
public static void main(String[] args)
{
int[] var={-11,-11,-11,-11,115,-11,-9};
int largest = 0;
int secLargest = 0;
if(var.length == 1)
{
largest = var[0];
secLargest = var[0];
}
else if(var.length > 1)
{
largest= var[0];
secLargest = var[1];
for(int i=1;i<var.length;i++)
{
if(secLargest!=largest)
{
if(var[i]>largest)
{
secLargest = largest;
largest = var[i];
}
else if(var[i]>secLargest && var[i] != largest)
{
secLargest= var[i];
}
}
else
{
if(var[i]>largest)
{
secLargest = largest;
largest = var[i];
}
else
{
secLargest = var[i];
}
}
}
}
System.out.println("Largest: "+largest+" Second Largest: "+secLargest);
}
}
答案 16 :(得分:0)
/* Function to print the second largest elements */
void print2largest(int arr[], int arr_size)
{
int i, first, second;
/* There should be atleast two elements */
if (arr_size < 2)
{
printf(" Invalid Input ");
return;
}
first = second = INT_MIN;
for (i = 0; i < arr_size ; i ++)
{
/* If current element is smaller than first
then update both first and second */
if (arr[i] > first)
{
second = first;
first = arr[i];
}
/* If arr[i] is in between first and
second then update second */
else if (arr[i] > second && arr[i] != first)
second = arr[i];
}
if (second == INT_MIN)
printf("There is no second largest elementn");
else
printf("The second largest element is %dn", second);
}
答案 17 :(得分:0)
我有最简单的逻辑来找到第二大数字,但事实并非如此。 逻辑在数组中找到具有最高值的两个数字的总和,然后检查哪个在两个简单中更大............
int ar[]={611,4,556,107,5,55,811};
int sum=ar[0]+ar[1];
int temp=0;
int m=ar[0];
int n=ar[1];
for(int i=0;i<ar.length;i++){
for(int j=i;j<ar.length;j++){
if(i!=j){
temp=ar[i]+ar[j];
if(temp>sum){
sum=temp;
m=ar[i];
n=ar[j];
}
temp=0;
}
}
}
if(m>n){
System.out.println(n);
}
else{
System.out.println(m);
}
答案 18 :(得分:0)
import java.util.Scanner;
public class SecondHighestFromArrayTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter size of Array");
int size = scan.nextInt();
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = scan.nextInt();
}
System.out.println("second highest element " + getSecondHighest(arr));
}
public static int getSecondHighest(int arr[]) {
int firstHighest = arr[0];
int secondHighest = arr[0];
for (int i = 0; i < arr.length; i++) {
if (arr[i] > firstHighest) {
secondHighest = firstHighest;
firstHighest = arr[i];
} else if (arr[i] > secondHighest) {
secondHighest = arr[i];
}
}
return secondHighest;
}
}
答案 19 :(得分:0)
最简单的方法是 -
public class SecondLargest {
public static void main(String[] args) {
int[] arr = { 1, 2, 5, 6, 3 };
int first = Integer.MIN_VALUE;
int second = Integer.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
// If current element is smaller than first then update both first
// and second
if (arr[i] > first) {
second = first;
first = arr[i];
}
// If arr[i] is in between first and second then update second
else if (arr[i] > second && arr[i] != first) {
second = arr[i];
}
}
}
}
答案 20 :(得分:0)
数组中的第二大元素: 在爪哇:
class test2{
public static void main(String[] args) {
int a[] = {1,2,3,9,5,7,6,4,8};
Arrays.sort(a);
int aa = a[a.length -2 ];
System.out.println(aa);
}//main
}//end
在Python中:
a = [1, 2, 3, 9, 5, 7, 6, 4, 8]
aa = sorted(list(a))
print(aa)
aaa = aa[-2]
print(aaa)
答案 21 :(得分:0)
此Java程序有助于查找任何给定数组中的第二大数字。 首先,我们必须获得最高的价值,然后我们必须取得第二的价值。
$result = DB::table('newsfeed_posts')
->select('newsfeed_posts.*', 'users.first_name as posted_user_first_name', 'users.last_name as posted_user_last_name', 'users.profile_image as posted_user_profile_image','proid.profile_image as posted_receiver_profile_image', 'timeline.first_name as post_receiver_first_name', 'timeline.last_name as post_receiver_last_name', 'timeline.office_branch_id as post_receiver_office_id')
->leftJoin('users', 'users.id', 'newsfeed_posts.post_sender_id')
->leftJoin('users as timeline', 'timeline.id', 'newsfeed_posts.post_receiver_id')
->leftJoin('users as proid', 'proid.id', 'newsfeed_posts.post_receiver_id')
->where('newsfeed_posts.deleted_status', '0')
// ->where('newsfeed_posts.post_sender_id', Auth::user()->id)
->where('newsfeed_posts.post_receiver_id', Auth::user()->id)
->groupBy('newsfeed_posts.id')
->orderBy('newsfeed_posts.id', 'DESC')
->get();
return [
'total' => $result->count(),
'result' => $result
];
答案 22 :(得分:0)
private static void returnSecondHighetsNumber(int[] a) {
int firstHighest=0;
int secondHighest=0;
if(a[0]>=a[1]) {
firstHighest=a[0];
secondHighest=a[1];
}else {
firstHighest=a[1];
secondHighest=a[0];
}
for (int i = 2; i < a.length; i++) {
if(a[i] >=firstHighest) {
secondHighest=firstHighest;
firstHighest=a[i];
}
else if(a[i]>=secondHighest) {
secondHighest=a[i];
}
}
System.out.println(firstHighest+"---"+secondHighest);
}
答案 23 :(得分:0)
您也可以找到最大和第三大数量的未排序数组。
public class ThirdLargestNumber {
public static void main(String[] args) {
int arr[] = { 220, 200, 100, 100, 300, 600, 50, 5000, 125, 785 };
int first = 0, second = 0, third = 0, firstTemp = 0, secondTemp = 0;
for (int i = 0; i <= 9 /*
* Length of array-1. You can use here length
* property of java array instead of hard coded
* value
*/; i++) {
if (arr[i] == first) {
continue;
}
if (arr[i] > first) {
firstTemp = first;
secondTemp = second;
first = arr[i];
second = firstTemp;
if (secondTemp > third) {
third = secondTemp;
}
} else {
if ((arr[i] == second) || (arr[i]) == first) {
continue;
}
if ((arr[i] > second) && (arr[i]) < first) {
secondTemp = second;
second = arr[i];
if (secondTemp > third) {
third = secondTemp;
}
} else {
if (arr[i] > third) {
third = arr[i];
}
}
}
}
// System.out.println("Third largest number: " + third);
System.out.println("Second largest number: " + second);
// System.out.println("Largest number: " + first);
}
}
答案 24 :(得分:0)
private static int SecondBiggest(int[] vector)
{
if (vector == null)
{
throw new ArgumentNullException("vector");
}
if (vector.Length < 2)
{
return int.MinValue;
}
int max1 = vector[0];
int max2 = vector[1];
for (int i = 2; i < vector.Length; ++i)
{
if (max1 > max2 && max1 != vector[i])
{
max2 = Math.Max(max2, vector[i]);
}
else if (max2 != vector[i])
{
max1 = Math.Max(max1, vector[i]);
}
}
return Math.Min(max1, max2);
}
这会将重复项视为相同的数字。如果你想要所有最大的和第二大的重复,你可以改变条件检查。
答案 25 :(得分:0)
如果时间复杂度不是问题,那么您可以运行冒泡排序,并且在两次迭代中,您将得到第二个最高数字,因为在循环的第一次迭代中,最大数字将移动到最后一个。在第二次迭代中,第二个最大数字将在最后一个位置移动。
答案 26 :(得分:0)
如果你想要数组中的第二高和最高数字索引,那么......
public class Scoller_student {
public static void main(String[] args) {
System.out.println("\t\t\tEnter No. of Student\n");
Scanner scan = new Scanner(System.in);
int student_no = scan.nextInt();
// Marks Array.........
int[] marks;
marks = new int[student_no];
// Student name array.....
String[] names;
names = new String[student_no];
int max = 0;
int sec = max;
for (int i = 0; i < student_no; i++) {
System.out.println("\t\t\tEnter Student Name of id = " + i + ".");
names[i] = scan.next();
System.out.println("\t\t\tEnter Student Score of id = " + i + ".\n");
marks[i] = scan.nextInt();
if (marks[max] < marks[i]) {
sec = max;
max = i;
} else if (marks[sec] < marks[i] && marks[max] != marks[i]) {
sec = i;
}
}
if (max == sec) {
sec = 1;
for (int i = 1; i < student_no; i++) {
if (marks[sec] < marks[i]) {
sec = i;
}
}
}
System.out.println("\t\t\tHigherst score id = \"" + max + "\" Name : \""
+ names[max] + "\" Max mark : \"" + marks[max] + "\".\n");
System.out.println("\t\t\tSecond Higherst score id = \"" + sec + "\" Name : \""
+ names[sec] + "\" Max mark : \"" + marks[sec] + "\".\n");
}
}
答案 27 :(得分:0)
public class SecondHighest {
public static void main(String[] args) {
// TODO Auto-generated method stub
/*
* Find the second largest int item in an unsorted array.
* This solution assumes we have atleast two elements in the array
* SOLVED! - Order N.
* Other possible solution is to solve with Array.sort and get n-2 element.
* However, Big(O) time NlgN
*/
int[] nums = new int[]{1,2,4,3,5,8,55,76,90,34,91};
int highest,cur, secondHighest = -1;
int arrayLength = nums.length;
highest = nums[1] > nums[0] ? nums[1] : nums[0];
secondHighest = nums[1] < nums[0] ? nums[1] : nums[0];
if (arrayLength == 2) {
System.out.println(secondHighest);
} else {
for (int x = 0; x < nums.length; x++) {
cur = nums[x];
int tmp;
if (cur < highest && cur > secondHighest)
secondHighest = cur;
else if (cur > secondHighest && cur > highest) {
tmp = highest;
highest = cur;
secondHighest = tmp;
}
}
System.out.println(secondHighest);
}
}
}
答案 28 :(得分:0)
public class secondLargestElement
{
public static void main(String[] args)
{
int []a1={1,0};
secondHigh(a1);
}
public static void secondHigh(int[] arr)
{
try
{
int highest,sec_high;
highest=arr[0];
sec_high=arr[1];
for(int i=1;i<arr.length;i++)
{
if(arr[i]>highest)
{
sec_high=highest;
highest=arr[i];
}
else
// The first condition before the || is to make sure that second highest is not actually same as the highest , think
// about {5,4,5}, you don't want the last 5 to be reported as the sec_high
// The other half after || says if the first two elements are same then also replace the sec_high with incoming integer
// Think about {5,5,4}
if(arr[i]>sec_high && arr[i]<highest || highest==sec_high)
sec_high=arr[i];
}
//System.out.println("high="+highest +"sec"+sec_high);
if(highest==sec_high)
System.out.println("All the elements in the input array are same");
else
System.out.println("The second highest element in the array is:"+ sec_high);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Not enough elements in the array");
//e.printStackTrace();
}
}
}
答案 29 :(得分:0)
如果这个问题来自面试官那么请不要使用分类技术或者不要使用任何内置的方法,如Arrays.sort或Collection.sort。这个问题的目的是你的解决方案在性能方面的优化程度,因此最好的选择就是用你自己的逻辑实现O(n-1)实现。以下代码仅供初学者使用,不适合有经验的人。
public void printLargest(){
int num[] ={ 900,90,6,7,5000,4,60000,20,3};
int largest = num[0];
int secondLargest = num[1];
for (int i=1; i<num.length; i++)
{
if(largest < num[i])
{
secondLargest = largest;
largest = num[i];
}
else if(secondLargest < num[i]){
secondLargest = num[i];
}
}
System.out.println("Largest : " +largest);
System.out.println("Second Largest : "+secondLargest);
}
答案 30 :(得分:0)
如果我们可以使用内置功能
,我认为找到第二个最高,我们不需要这些线路int[] randomIntegers = {1, 5, 4, 2, 8, 1, 1, 6, 7, 8, 9};
Arrays.sort(randomIntegers);
System.out.println(randomIntegers[randomIntegers.length-2]);
答案 31 :(得分:0)
我提供的解决方案不在JAVA程序中(用JavaScript编写),但需要o(n / 2)次迭代才能找到最高和第二高的数字。
工作提琴手链接Fiddler link
var num=[1020215,2000,35,2,54546,456,2,2345,24,545,132,5469,25653,0,2315648978523];
var j=num.length-1;
var firstHighest=0,seoncdHighest=0;
num[0] >num[num.length-1]?(firstHighest=num[0],seoncdHighest=num[num.length-1]):(firstHighest=num[num.length-1], seoncdHighest=num[0]);
j--;
for(var i=1;i<=num.length/2;i++,j--)
{
if(num[i] < num[j] )
{
if(firstHighest < num[j]){
seoncdHighest=firstHighest;
firstHighest= num[j];
}
else if(seoncdHighest < num[j] ) {
seoncdHighest= num[j];
}
}
else {
if(firstHighest < num[i])
{
seoncdHighest=firstHighest;
firstHighest= num[i];
}
else if(seoncdHighest < num[i] ) {
seoncdHighest= num[i];
}
}
}
答案 32 :(得分:0)
var map;
var mapProp;
var marker;
var markers;
var url;
var myData;
var time;
var available;
var total;
var facility;
var position;
var infoWindow;
function initialize() {
mapProp = {
center: new google.maps.LatLng(38.994890, -77.063416),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map"), mapProp);
}
$(document).ready(function() {
initialize();
url = 'https://data.montgomerycountymd.gov/resource/qahs-fevu.json';
$.getJSON(url, function(data) {
myData = data;
for (i = 0; i < myData.length; i++) {
time = (myData[i].asofdatetime).slice(11);
available = myData[i].space_count;
total = myData[i].total_spaces;
facility = myData[i].facilitynumber;
if (facility === "GAR 57") {
facility = "4841 Bethesda Avenue (Elm Street)";
$('#GAR57').html('As of ' + time + ' there are ' + available +
' of ' + total + ' at ' + facility);
} else if (facility === "GAR 31") {
facility = "7171 Woodmont Avenue";
$('#GAR31').html('As of ' + time + ' there are ' + available +
' of ' + total + ' at ' + facility);
} else if (facility === "GAR 60") {
facility = "921 Wayne Avenue";
$('#GAR60').html('As of ' + time + ' there are ' + available +
' of ' + total + ' at ' + facility);
} else {
facility = "801 Ellsworth Drive";
$('#GAR61').html('As of ' + time + ' there are ' + available +
' of ' + total + ' at ' + facility);
}
}
});
//set markers
markers = [
["4841 Bethesda Avenue (Elm Street)", 38.980724, -77.0964],
["7171 Woodmont Avenue", 38.980097, -77.093662],
["921 Wayne Avenue", 38.995740, -77.025652],
["801 Ellsworth Drive", 38.997778, -77.025071]
];
infoWindow = new google.maps.InfoWindow();
for (var i = 0; i < markers.length; i++) {
console.log("markers: " + markers[i][0]);
position = new google.maps.LatLng(markers[i][1], markers[i][2]);
marker = new google.maps.Marker({
position: position,
map: map,
title: markers[i][0]
});
console.log("markers 2: " + markers[i][0]);
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent("<div>Hello, World" + markers[i][0] + "</div>");
infoWindow.open(map, this);
});
};
});
答案 33 :(得分:-1)
static int secondLargest(int[] input){
int largest , secondlargest;
if(input[0]>input[1])
{
largest=input[0];
secondlargest = input[1];
}
else
{
secondlargest = input[0];
largest =input[1];
}
for(int i =2 ;i<input.length;i++){
if(input[i]>largest)
{
secondlargest = largest;
largest = input[i];
}
else if(input[i]>secondlargest){
secondlargest = input[i];
}
}
return secondlargest;
}
答案 34 :(得分:-1)
我会提出更简单,更简单的问题答案,它在方法中包含2行代码(import java.util.Arrays
是必需的):
public static int secMax(int[] num){
Arrays.sort(num);
temp = num[num.length-2];
return temp;
}
答案 35 :(得分:-1)
public static void main(String[] args) {
int[] arr = {0,12,74,56,2,63,45};
int f1 = 1, f2 = 0, temp = 0;
int num = 0;
for (int i = 0; i < arr.length; i++){
num = arr[i];
if (f1 < num) {
temp = f1;
f1 = num;
num = temp;
}
if (f2 < num) {
temp = f2;
f2 = num;
num = temp;
}
}
System.out.println("First Highest " + f1 + " Second Highest " + f2 + " Third " + num);
}
答案 36 :(得分:-1)
很容易获得阵列中第二高的元素。我在下面显示了您的所有参考。希望这会有所帮助。
import java.util.Arrays;
public class Testdemo {
public static void main(String[] args) {
int[] numbers = {1, 5, 4, 2, 8, 1, 1, 6, 7, 8, 9};
Arrays.sort(numbers);
System.out.println("The second Higest Element :" + numbers[numbers.length-2]);
}
}
Ans - 第二个最高元素:8
答案 37 :(得分:-1)
找到第二大数字:
public class SecondMaxNumbers {
public void printTwoMaxNumbers(int[] nums){
int maxOne = 0;
int maxTwo = 0;
for(int n:nums){
if(maxOne < n){
maxTwo = maxOne;
maxOne =n;
} else if(maxTwo < n){
maxTwo = n;
}
}
System.out.println("Second Max Number: "+maxTwo);
}
public static void main(String a[]){
int num[] = {10,20,30,40,50,60,70};
SecondMaxNumbers sec = new SecondMaxNumbers();
tmn.printTwoMaxNumbers(num);
}
}
答案 38 :(得分:-2)
Scanner sc = new Scanner(System.in);
System.out.println("\n number of input sets::");
int value=sc.nextInt();
System.out.println("\n input sets::");
int[] inputset;
inputset = new int[value];
for(int i=0;i<value;i++)
{
inputset[i]=sc.nextInt();
}
int maxvalue=inputset[0];
int secondval=inputset[0];
for(int i=1;i<value;i++)
{
if(inputset[i]>maxvalue)
{
maxvalue=inputset[i];
}
}
for(int i=1;i<value;i++)
{
if(inputset[i]>secondval && inputset[i]<maxvalue)
{
secondval=inputset[i];
}
}
System.out.println("\n maxvalue"+ maxvalue);
System.out.println("\n secondmaxvalue"+ secondval);
答案 39 :(得分:-2)
这是我在C,O(N)复杂性时间内的答案。 仅传递一次数组,只传递三个变量。 该解决方案非常直观且易于理解。
#include <stdio.h>
int second(int arr[],int size)
{
int i, max , secondmax;
if (arr[0] > arr[1]){
max = arr[0];
secondmax = arr[1];
} else {
max = arr[1];
secondmax = arr[0];
}
for (i = 2; i < size; i++)
{
if ((arr[i] < max) && (arr[i] < secondmax)) {
continue;
}
if ((arr[i] < max) && (arr[i] > secondmax)) {
secondmax = arr[i];
continue;
}
if ((arr[i] > max)) {
secondmax = max;
max = arr[i];
continue;
}
}
return secondmax;
}
void main()
{
int arr[] = { 1,10,5,7};
int size = sizeof(arr) / sizeof(arr[0]);
int secondmax = second(arr,size);
printf("%d \n\n", secondmax);
}
答案 40 :(得分:-2)
int arr[] = {25, 5, 35, 26, 6, 270, 0};
int large1 = arr[0];
int large2 = arr[1];
for (int i = 2; i < arr.length; i++) {
if (arr[i] > large1) {
if (large1 > large2)
large2 = large1;
large1 = arr[i];
} else if (arr[i] > large2)
large2 = arr[i];
}
System.out.println("Large1 " + large1);
System.out.println("Large2 " + large2);
答案 41 :(得分:-2)
请在下面找到此问题的确切答案
int array[]={1,3,55,33,22,55,66,77,54,77,44,35,22,75};
// Initialize these to the smallest value possible
Arrays.sort(array);
System.out.println(array[array.length-1]);
for (int i = array.length-1; i >0; i--) {
if(array[i]==array[--i]){
continue;
}else{
System.out.println("Second Largest In Array"+array[i]);
break;
}
}