当前位置: 代码迷 >> 综合 >> hdu 1151 Cable master (好坑的精度。。。)
  详细解决方案

hdu 1151 Cable master (好坑的精度。。。)

热度:83   发布时间:2024-01-13 20:58:35.0

1、http://acm.hdu.edu.cn/showproblem.php?pid=1551

2、题目:给定一些列长度的绳子,要求将其剪成K段,K段相等,求每段最长多少?

3、poj过的代码,用的取整,hdu不过。。。。

#include<stdio.h>
#define N 10005
double a[N];
double sum;
int n,k;
int find(int mid)
{int count=0;for(int i=0;i<n;i++){count+=(int)((a[i]*100)/mid);}//printf("copunt=%d\n",count);if(count>=k)return 1;elsereturn 0;
}
int main()
{double max;while(scanf("%d%d",&n,&k)!=EOF){max=-1;if(n==0 && k==0)break;for(int i=0;i<n;i++){scanf("%lf",&a[i]);if(a[i]>max)max=a[i];sum+=a[i];}if(sum*100<k)printf("0.00\n");else{int left=0;int right=int(max*100);int mid;//printf("*%d %d\n",left,right);while(left<=right){mid=(left+right)>>1;//printf("*****%d\n",mid);if(find(mid))left=mid+1;elseright=mid-1;}printf("%.2lf\n",right*1.0/100);}}return 0;
}
/*
4 11
8.02
7.43
4.57
5.39
0 0
*/


 

2、hdu 过的代码,用小数做。。。。

#include<stdio.h>
#define N 10005
double a[N];
double sum;
int n,k;
int find(double mid)
{int count=0;for(int i=0;i<n;i++){count+=(int)(a[i]/mid);}//printf("copunt=%d\n",count);if(count>=k)return 1;elsereturn 0;
}
int main()
{double max;while(scanf("%d%d",&n,&k)!=EOF){max=-1;if(n==0 && k==0)break;for(int i=0;i<n;i++){scanf("%lf",&a[i]);if(a[i]>max)max=a[i];sum+=a[i];}if(sum*100<k)printf("0.00\n");else{double left=0;double right=max*100;double mid;//printf("*%d %d\n",left,right);while(right-left>1e-5){mid=(left+right)/2;//printf("*****%d\n",mid);if(find(mid))left=mid;elseright=mid;}printf("%.2lf\n",right);}}return 0;
}
/*
4 11
8.02
7.43
4.57
5.39
0 0
*/


 

 

 

  相关解决方案