二分法求解木棒切割问题

#include
using namespace std;
int f(int x){
int a = 10/x;
int b = 24/x;
int c = 15/x;
int d = a+b+c;
return d;
}
int main(){
int left = 0,right = 10,mid;
while(left mid = (left+right)/2;
if(f(mid)==7){
printf("%d",mid);
break;
}
else if(f(mid)>7){ //分段长度小了
left = mid;
}
else {
right = mid;
}
}
return 0;
}

如有错误,欢迎指出,共同进步

你可能感兴趣的:(二分法求解木棒切割问题)