这题精度控制是难点,方法很简单,二分查找
/**
* @description: poj1064,多根电缆切成指定段数(相同长度),求每段最大长度
* @author: michael ming
* @date: 2019/5/2 15:14
* @modified by:
*/
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <cmath>
using namespace std;
int main()
{
int cables, target, cable_we_get;
cin >> cables >> target;
double minlen = 0.0, maxlen = 100000, mid, len[10001];
for(int i = 0; i < cables; ++i)
{
// cin >> len[i];
scanf("%lf", &len[i]);
// len[i] += 1E-15;
// maxlen = maxlen > len[i] ? maxlen : len[i];
}
while(maxlen - minlen > 1E-3)
{
cable_we_get = 0;
mid = minlen + (maxlen - minlen)/2;
for(int i = 0; i < cables; ++i)
{
cable_we_get += (int)(len[i]/mid);
}
if(cable_we_get >= target)
minlen = mid;
else
maxlen = mid;
}
// mid = double(floor(mid*100))/100.0;
// cout << fixed << setprecision(2) << mid << endl;
printf("%.2lf\n", floor(mid*100)/100);
return 0;
}
/**
* @description: poj1064,多根电缆切成指定段数(相同长度),求每段最大长度
* @author: michael ming
* @date: 2019/5/2 15:14
* @modified by:
*/
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <cmath>
using namespace std;
int main()
{
int cables, target, cable_we_get;
cin >> cables >> target;
double minlen = 0.0, maxlen = 100000, mid, len[10001];
for(int i = 0; i < cables; ++i)
{
cin >> len[i];
// scanf("%lf", &len[i]);
}
while(maxlen - minlen > 1E-3)
{
cable_we_get = 0;
mid = minlen + (maxlen - minlen)/2;
for(int i = 0; i < cables; ++i)
{
cable_we_get += (int)(len[i]/mid);
}
if(cable_we_get >= target)
minlen = mid;
else
maxlen = mid;
}
cout << fixed << setprecision(2) << floor(maxlen*100)/100.0 << endl;
// printf("%.2lf\n", floor(maxlen*100)/100);
// 以上两种写法都可以AC,但是注意表达式内不要写mid,要写maxlen
// 最后如果是mid=1.999,保留两位,直接是1.99
// maxlen是2.001,直接保留2.00
return 0;
}
/**
* @description: poj1064 数字放大100倍做法,输入浮点数分成整数部分和小数部分,避免1.50输入后变成1.4999999
* @author: michael ming
* @date: 2019/5/2 20:00
* @modified by:
*/
#include <iostream>
using namespace std;
int main()
{
int cables, target, cable_we_get;
cin >> cables >> target;
int minlen = 1, maxlen = 10000000, mid, len[10001], ans;
int int_part, float_part;
char ch;
for(int i = 0; i < cables; ++i)
{
cin >> int_part >> ch >> float_part;
len[i] = int_part * 100 + float_part;
}
while(maxlen - minlen >= 0)
{
cable_we_get = 0;
mid = minlen + (maxlen - minlen)/2;
for(int i = 0; i < cables; ++i)
{
cable_we_get += len[i]/mid;
}
if(cable_we_get >= target)
minlen = mid+1;
else
maxlen = mid-1;
}
cout << maxlen/100 << "." << (maxlen/10)%10 << maxlen%10 << endl;
return 0;
}