2 Holzbrett | Huawei od Computer Test

Originallink:[Vollständige Partitur][Huawei OD Machine Test Real Questions 2023 JAVA&JS] Wooden Board_Ruo Bodous Blog-CSDN-Blog

C++-Code: 

#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;
}

Guess you like

Origin blog.csdn.net/weixin_65293439/article/details/129827651