桌子上有N个袋子,每个袋子里有i(i∈[0,10000])个球,输入分钟数M,每分钟只能拿一个袋子里的球,求每分钟最少拿多少球才能在规定的时间内将所有球拿完(当输入错误,输出-1)。
输入如下:
3 13 5 7 8
表示:有4个袋子,每个袋子里的球数分别为3、13、5、7。分钟数为8分钟
输出:
4
#include<stdio.h>
#include<stdlib.h>
typedef struct _SLNode
{
int val;
struct _SLNode* Next;
}SLNode;
int main()
{
int iInput;
SLNode* head = (SLNode*)malloc(sizeof(SLNode));
if (head == NULL)
{
return 0;
}
head->Next = NULL;
int iMax = 0;
while (scanf("%d", &iInput) != EOF)
{
SLNode* pCurNode = (SLNode*)malloc(sizeof(SLNode));
if (pCurNode == NULL)
{
return 0;
}
pCurNode->val = iInput;
pCurNode->Next = head->Next;
head->Next = pCurNode;
}
if ((head == NULL) || (head->Next == NULL))
{
printf("%d", -1);
return 0;
}
SLNode* pTempNde = head->Next;
int iHour = pTempNde->val;
SLNode* pCur = pTempNde->Next;
while (pCur)
{
if (iMax < pCur->val)
{
iMax = pCur->val;
}
pCur = pCur->Next;
}
int iMinSpeed = 0;
for (int i = 1; i <= iMax; i++)
{
int iTemp = 0;
pCur = pTempNde->Next;
while (pCur)
{
if (pCur->val == 0)
{
iTemp += 0;
}
else if (((pCur->val) % i) == 0)
{
iTemp += ((pCur->val) / i);
}
else
{
iTemp += (((pCur->val) / i) + 1);
}
pCur = pCur->Next;
}
if (iTemp <= iHour)
{
iMinSpeed = i;
break;
}
}
if (iMinSpeed == 0)
{
printf("%d", -1);
}
else
{
printf("%d", iMinSpeed);
}
return 0;
}