나무판 2개 | 화웨이 od 컴퓨터 테스트

원본 링크:[전체 점수][Huawei OD 기계 테스트 실제 질문 2023 JAVA&JS] 나무판_Ruo Bodou의 블로그-CSDN 블로그

C++ 코드: 

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 100010;

int n, m;
int a[N];

int main()
{
    scanf("%d%d", &n, &m);
    
    int min = 0;
    
    for(int i = 0; i < n; i ++ ) 
        scanf("%d", &a[i]);
    
    sort(a , a + n);//从小到大排序
    
    while(m -- ) {
        for(int i = 1; i < n; i ++ ) {
            if(a[i] > a[i - 1])//如果比前一个长, 前一个+1
            {
                a[i - 1] ++ ;//
                break;
            }//否则 不加
            if(i == n - 1)//遍历到尾巴了, 就最后一节+1
            {
                a[i] ++ ;
            }
        }
        
    }
    
    cout << a[0] << endl;
    
    return 0;
}

추천

출처blog.csdn.net/weixin_65293439/article/details/129827651