2 solutions

  • 8
    @ 2024-12-24 15:49:16

    从前往后遍历整个序列,如果当前数比最大值大,那么当前数就是最大值,已经的最大值就变成了次大值,否则如果当前数大于次大值,那么就只更新次大值

    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        int n;
        cin>>n;
        int maxv1=0,maxv2=0;
        for(int i=1;i<=n;i++)
        {
        	int x;
        	cin>>x;
        	if(x>maxv1) //当前数比最大值大,更新最大值和次大值 
    		{
    			maxv2=maxv1,maxv1=x;
    		}
    		else if(x>maxv2) //当前数比次大值大,更新次大值 
    		{
    			maxv2=x;
    		}
    	}
    	cout<<maxv1<<" "<<maxv2;
        return 0;
    }
    
    • 1
      @ 2025-9-10 22:12:57

      这方法包牛鼻的

      #include<bits/stdc++.h>
      using namespace std;
      int main(){
      	int a;
      	cin>>a;
      	int n[100000];
      	for(int i=1;i<=a;i++){
      		cin>>n[i];
      	}
      	sort(n,n+a+1);
      	cout<<n[a]<<" "<<n[a-1];
      	return 0;
      }
      
      
      
      • 1

      Information

      ID
      2138
      Time
      1000ms
      Memory
      256MiB
      Difficulty
      3
      Tags
      # Submissions
      131
      Accepted
      49
      Uploaded By