輸入2個數 n k
n條繩子 要分成大於等於k段
求每段最長多長呢?並且每段不能小於1cm
必須以釐米精度寫入數字,小數點後正好是兩位數。
如果無法切割所請求的每個長度至少爲1釐米的件數,則輸出文件必須包含單個數字“0.00”(不帶引號)。
Sample Input
4 11
8.02
7.43
4.57
5.39
Sample Output
2.00
4條繩子 分成大於等於11段
用二分的方法分(需要注意的是,分的繩子的長度不一定是最短的那條繩子,也就是不需要每條繩子都裁剪)
比如4條繩子 分2段
上面的題的答案就是7.00
而且 這個輸出是個坑點,不需要進,我看其他大佬的方法是,輸入時先*100輸出的時候/100就能解決這個輸出問題
上代碼
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
#define N 10010
#define MAX 10000000
int a[N];
int main(){
int n ,m;
double len;
while (scanf("%d%d",&n,&m)!=EOF){
int Max=0;
for(int i=0;i<n;i++)
{
scanf("%lf",&len);
a[i]=len*100;
Max=max(Max,a[i]);
}
int low=1,high=Max;//1釐米
int res=0;
while(low<=high)
{
int mid =(low+high)>>1;//值右移1位,相當l+r的值除以2取整。
int count =0;
for(int i=0;i<n;i++)
{
count+=a[i]/mid;//每根繩子能剪成幾段
}
if(count>=m)//如果大於等於需要剪的個數則,讓
res=max(res,mid),low=mid+1;//因爲要找最長的線段所以需要max函數比較
else
high=mid-1;
}
printf("%.2f\n",(double)res/100.0);
}
return 0;
}