[Mảng 1 Chiều Cơ Bản]. Bài 15. Số lớn nhất, lớn thứ 2

View as PDF

Submit solution

Points: 1.00 (partial)
Time limit: 1.0s
Java 2.0s
Memory limit: 256M
Input: stdin
Output: stdout

Author:
Problem source:
28Tech
Problem type
Allowed languages
C, C#, C++, Java, Kotlin, Pascal, PyPy, Python, Scratch

Cho mảng số nguyên A[] gồm N phần tử, tìm số lớn nhất và lớn thứ 2 trong mảng. Chú ý 2 giá trị này có thể giống nhau


Đầu vào

Dòng đầu tiên là số nguyên dương N

Dòng thứ 2 gồm N số nguyên viết cách nhau một vài khoảng trắng


Giới hạn

2<=N<=10^6

0<=A[i]<=10^6


Đầu ra

In ra số lớn thứ nhất và lớn thứ 2 trong mảng


Ví dụ :

Input 01
5
1 2 3 4 5
Output 01
5 4
Input 02
5
1 5 5 4 5
Output 02
5 5

Comments

Please read the guidelines before commenting.



  • -1
    Nguyen_Van_Tien_ccp012  commented on Aug. 24, 2024, 2:22 a.m.

    include <iostream>

    using namespace std;

    int main() { int n;cin >> n; int a[n]; for (int i = 0;i < n;i++) { cin >> a[i]; } int m1 = 0,m2 = 0; for (int i = 0;i < n;i++) { if (m1 < a[i]) m1 = a[i]; else if (m1 == a[i]) m2 = m1; } for (int i = 0;i < n;i++) { if (m2 < a[i] && a[i] != m1) m2 = a[i]; } cout << m1 << " " << m2 << endl; }


  • -5
    Itachi  commented on April 29, 2024, 11:06 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -1
    xuanthinh  commented on April 28, 2024, 1:44 a.m.

    Mn có thể tham khảo code của mình :

    include <bits/stdc++.h>

    using namespace std;

    define ll long long

    ll maxx1=0,maxx2=0,n,x; int main() { iosbase::syncwith_stdio(false); cin.tie(NULL); cin>>n; for(int i=0; i<n; i++) { cin>>x; if(x>=maxx1) { maxx2=maxx1; maxx1=x; } else if(x>=maxx2) { maxx2=x; } } cout<<maxx1<<" "<<maxx2<