[Mảng 1 Chiều Cơ Bản]. Bài 12. Vị trí số lớn nhất, nhỏ nhất

View as PDF

Submit solution

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

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ử, hãy tìm vị trí(bắt đầu từ 0) cuối cùng của giá trị nhỏ nhất trong mảng và vị trí đầu tiên của giá trị lớn nhất trong mảng.

Tức là nếu có nhiều số có cùng giá trị nhỏ nhất bạn phải in ra ví trí cuối cùng, và có nhiều số có cùng giá trị lớn nhất trong mảng bạn phải in ra vị trí đầu tiên lớn nhất đó.


Đầ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

1<=N<=10^6

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


Đầu ra

In trên 1 dòng 2 chỉ số mà bạn tìm được.


Ví dụ :

Input 01
9
936 234 471 168 834 82 280 674 881
Output 01
5 0

Comments

Please read the guidelines before commenting.



  • 0
    gryphware  commented on Nov. 24, 2024, 2:02 p.m.

    include <stdio.h>

    include <limits.h>

    int main(){

    int indexOfMin, indexOfMax;
    int min = INT_MAX, max = INT_MIN;
    
    int size; 
    scanf("%d", &size);
    
    for(size_t i = 0; i < size; i++){
        int num;
        scanf("%d", &num);
    
        if(min > num){
            min = num;
            indexOfMin = i;
        } else if (min == num){
            indexOfMin = i;
        }
        if(max < num){
            max = num;
            indexOfMax = i;
        }
    }
    
    printf("%d %d", indexOfMin, indexOfMax);
    
    return 0;
    

    }


  • -1
    ichingu  commented on Nov. 6, 2024, 1:50 p.m.


  • 0
    khanhhduy_1305  commented on Sept. 14, 2024, 3:52 a.m.

    include<bits/stdc++.h>

    using namespace std; int main() { int n,x; vector<int> v; cin>>n; int maxn,minn; for(int i=0;i<n;i++) { cin>>x; v.pushback(x); } auto it=maxelement(v.begin(),v.end()); auto ki=min_element(v.begin(),v.end()); for(int i=0;i<v.size();i++) { if(v[i]==ki) minn=i; } for(int i=0;i<v.size();i++) { if(v[i]==it) {maxn=i;break;} } cout<<minn<<" "<<maxn; }


  • -10
    theguy777_jaboi  commented on July 31, 2024, 3:01 a.m.

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


  • -6
    kyvynguyen2006  commented on July 28, 2024, 6:38 a.m.

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


  • 2
    truongkiritokun  commented on July 13, 2024, 12:32 p.m.

    using System; using System.Net;

    namespace Work { internal class MainControl {

        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            string[] w = Console.ReadLine().Split(' ');
            int[] p = new int[n];
            for (int i = 0; i < n; i++)
            {
                p[i] = int.Parse(w[i]);
            }
            solve((arr) =>
            {
                int min = int.MaxValue, idxmin = 0;
                int max = int.MinValue, idxmax = 0;
                for (int i = 0; i < arr.Length; i++)
                {
                    if(arr[i] <= min)
                    {
                        min = arr[i];
                        idxmin = i;
                    }
                    if (arr[i] > max)
                    {
                        max = arr[i];
                        idxmax = i;
                    }
                }
                Console.WriteLine($"{idxmin} {idxmax}");
            }, p);
        }
        static void solve(Action<int[]> act, int[] a) => act(a);
    }
    

    }


  • -3
    longhk2012  commented on July 12, 2024, 6:14 a.m.

    .


    • -4
      longhk2012  commented on July 12, 2024, 6:14 a.m.

      bai nay kho hieu qua


      • 0
        kytoLam  commented on July 21, 2024, 4:04 p.m.

        hm,


  • -3
    hackerlo2803  commented on June 30, 2024, 5:30 p.m.

    .


  • -15
    Itachi  commented on May 1, 2024, 1:03 a.m.

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