今日头条2018 AI Camp笔试编程题——最小分割分数

在这里插入图片描述
代码:

#include <iostream>
using namespace std;
int max_ = 0;
int sum = 0;
int sum_temp = 0;
int count_ = 0;
//n is the length of arr,m is the number of your split.
int func(int* arr,int n,int m)
{
    for(int i=0;i<n;++i)//this loop is to get the max and sum,is also the floor and ceil of next loop.
    {
        if(arr[i]>max_)
        {
            max_ = arr[i];
        }
        sum+=arr[i];
    }
    for(int j=max_;j<=sum;j++)//between the max and sum,we make a loop to get the smallest number that meets the condition.
    {
        for(int k=0;k<n;k++)
        {
            sum_temp += arr[k];
            if (sum_temp > j)//when the current sum of this part is bigger than j,we split.
            {
                count_++;
                sum_temp = arr[k];
            }
        }
        if(count_ < m)//when count_ < m,we say this j meets the condition.Because since you can split it by count_ times,
            // and count_<=m-1,so you must be able to split it by m-1 times,and of course get m parts, the sum of each part <=j.
        {
            return j;
        }

        else
        {
            count_ = 0;// if there is no count_<m,we set the count_ 0.and j+=1,begin next loop,until we get the smallest j
            //that meets the condition.
            sum_temp = 0;
        }
    }
    return sum;//if all cases can't meet the condition,so the sum of arr will be returned.
}
int main(){
    int array_test[5] = {1,4,2,3,5};
    cout<<func(array_test,5,3);
    return 0;
}

参考:
https://blog.csdn.net/seniusen/article/details/80539553

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值