0.Tìm hình chữ nhật có diện tích lớn nhì


Submit solution

Points: 2
Time limit: 1.0s
Memory limit: 98M

Author:
Problem type
Allowed languages
Ada, Assembly, Awk, C, C++, C11, CLANG, CLANGX, Classical, COBOL, Coffee, CSC, D lang, DART, F95, FORTH, Fortrn, GAS32, GO, Haskell, Itercal, Java, kotlin, LEAN, LISP, LUA, MONOVB, Nasm, OCAML, Pascal, Perl, php, PIKE, prolog, Pypy, Python, Ruby 2, RUST, Scala, SCM, SED, SWIFT, TCL, TUR, V8JS, VB, ZIG

Viết chương trình nhập vào kích thước của n hình chữ nhật (HCN). Chỉ ra kích thước của một HCN có diện tích lớn nhì trong danh sách HCN vừa nhập.

Đầu vào:

Dòng thứ nhất là số nguyên n (1<=n<=100) n dòng tiếp theo, mỗi dòng gồm 2 số nguyên là kích thước của một HCN.

Đầu ra:

Gồm 2 số nguyên là kích thước HCN có diện tích lớn nhì. (thứ tự của 2 số in ra giống với lúc nhập vào). Nếu không có HCN thỏa mãn thì in ra NO.

Ví dụ 1:

Input:

3
5 2
4 3
5 1

Output:

5 2

Ví dụ 2:

Input:

2
5 6
6 5

Output:

NO
Phan Hữu Tuấn

Comments


  • 0
    NguyenGiaPhu_CNTT3_K62  commented on Dec. 9, 2021, 2:16 a.m.

    Anh chị cho em xin in out test cuối với ạ


    • 3
      sguenm  commented on Dec. 9, 2021, 12:09 p.m. edit 3

      5 9 7 8 8 7 4 7


  • 1
    manh_ĐTVT5_K60  commented on Oct. 22, 2020, 1:49 p.m. edited
    #include<iostream>
    #include<math.h>
    using namespace std;
    struct HCN {
        long chieudai,chieurong,dientich;
        };
    typedef struct HCN hcn;
    void sapxep(hcn a[],long n){
       long i,j;
       hcn last;
       for(i=1;i<n;i++)
       {
        j=i;
        last=a[i];
        while(j>0&&a[j-1].dientich>last.dientich)
        {
            a[j]=a[j-1];
            j--;
            a[j]=last;
           }
       }
    }   
    int main()
    {
        long  n;cin>>n;
        hcn *a=new hcn[n];
        for(long i=0;i<n;i++)
        {
            cin>>a[i].chieudai>>a[i].chieurong;
            a[i].dientich=a[i].chieudai*a[i].chieurong;
        }
        sapxep(a,n);
        if(a[1].dientich==a[2].dientich)
        cout<<"NO";
        else
        cout<<a[1].chieudai<<" "<<a[1].chieurong;
        return 0;
    }
    // bài này hại não quá :)))))

  • 1
    enoughtodie99  commented on Nov. 2, 2018, 5:37 p.m. edited
    #include <bits/stdc++.h>
        using namespace std;
        struct HCN{
        int a,b,s;
        };
        bool ss(HCN A, HCN B)
        {
        return A.s < B.s;
        }
        int main()
        {
        int n;
        cin >> n;
        int k = n-2;
        HCN *a;
        a = new HCN[n];
        for(int i = 0; i < n; i ++)
            {
            cin >> a[i].a >> a[i].b;
            a[i].s = a[i].a*a[i].b;
            }
        sort(a,a+n,ss);
        while(k >= 0)
            {
            if(a[k].s < a[n-1].s && ((a[k].s!=a[k-1].s && k > 0) || k == 0))
                {
                cout << a[k].a << " " << a[k].b;
                break;
                }
            else
                k --;
            }
        if(k<0)
            cout << "NO";
        }

  • 1
    HELLOWORLD  commented on July 10, 2018, 10:17 a.m.

    ai giúp e bài này với. e thử hết các trường hợp k sai mà sao k được điểm tối đa thanks ạ


  • 1
    HELLOWORLD  commented on July 10, 2018, 10:14 a.m. edit 3
    #include<stdio.h>
        #include<math.h>
        void Sapxep(int* a, int* b);
        struct HCN
        {
            int x[100];
            int y[100];
            int s[100];
        };
        int main()
        { 
        int n,i,j,k=0,t,a[100],b[100],d[100],maxnhi,maxnhat,idx,ok=0;
         struct HCN arr[100];
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {   
        scanf("%d%d",&arr[i].x[i],&arr[i].y[i]);    
        }
        for(i=0;i<n;i++)
        {
            arr[i].s[i] = arr[i].x[i] * arr[i].y[i];
        }
    
         //sap xep day tang dan
        for(i=0;i<n-1;i++)
        { 
          for(j=i+1;j<n;j++)
          {
          if(arr[i].s[i]>arr[j].s[j])
          {
          Sapxep( &arr[i].s[i], &arr[j].s[j]);
          Sapxep( &arr[i].x[i], &arr[j].x[j]);
          Sapxep( &arr[i].y[i], &arr[j].y[j]);  
          }
          }
        }
        maxnhat= arr[n-1].s[n-1];
        maxnhi= arr[n-2].s[n-2];
        idx=n-2;
         if(maxnhi==maxnhat)
         {
         // maxnhat=d[i];
        maxnhi= arr[n-3].s[n-3];
            idx=n-3;
         }
        for(i=0;i<n;i++)
        {   
         if((arr[i].s[i]==arr[i+1].s[i+1]) && (arr[i].s[i]==arr[0].s[0])&&(arr[i].s[i]==arr[n-1].s[n-1]))
         {
         ok=1;
         }
        }
         if(ok==0)
         printf("\%d %d",arr[idx].x[idx], arr[idx].y[idx]);
         else
         printf("\nNO");
        return 0;
        }
    
        void Sapxep(int* a, int* b)
        {
            int temp;
            temp= *a;
            *a=*b;
            *b= temp;
        }

  • 3
    TICHPX  commented on July 8, 2017, 3:38 a.m.

    Trong trường hợp có nhiều cái cùng diện tích nhì thì in ra cái nào? Chẳng hạn 3 hình chữ nhật (3,12), (4,9), (10,5) thì in ra (3,12) hay (4,9).


  • 2
    killer98xxx  commented on July 5, 2017, 5:36 p.m.

    ai giúp với ạ, không hiểu tại sao test cuối sai :)


  • 1
    killer98xxx  commented on July 5, 2017, 5:35 p.m.
    #include<iostream>
        #include<string>
    
        using namespace std;
    
        typedef struct
        {
            int x;
            int y;
            int s;
        }HCN;
    
        int Tong(int n)
        {
            int s = 0;
            for (int i = 0; i < n; i++)
            {
                s += i;
            }
            return s;
        }
    
        int main()
        {
            int n;
            HCN A[30];
            cin >> n;
            for (int i = 0; i < n; i++)
            {
                cin >> A[i].x >> A[i].y;
                A[i].s=A[i].x*A[i].y;
            }
    
            int d=0;
    
            for (int i = 0; i < n-1; i++)
                for (int j = i+1; j < n; j++)
                {
                    if (A[i].s == A[j].s)
                    {
                        d++;
                    }
                }
            cout << d << endl;
    
            if (d == Tong(n))   cout << "NO" << endl;
            else
            {
                HCN temp;
                for (int i = 0; i < n - 1; i++)
                    for (int j = i + 1; j < n; j++)
                    {
                        if (A[i].s < A[j].s)
                        {
                            temp = A[i];
                            A[i] = A[j];
                            A[j] = temp;
                        }
                    }
                cout << A[1].x <<" "<<  A[1].y  << endl;
            }
            system("pause");
        }