2017TenXun校招(9-11)

作者:feilengcui008
链接:http://www.zhihu.com/question/31855632/answer/54747429
来源:知乎
著作权归作者所有,转载请联系作者获得授权。

#include <stdio.h>
int max(const int &a, const int &b){
    return a>b ? a : b;
}
int min(const int &a, const int &b){
    return a>b ? b : a;
}
void mball_nfloor(int m, int m);
int main(int argc, char **argv){
    fprintf(stdout, "\n**********************mball_nfloor(4, 100)************************\n");
    mball_nfloor(4, 100);
    fprintf(stdout,
"\n******************************************************************\n");
    fprintf(stdout, "\n**********************mball_nfloor(1, 100)************************\n");
    mball_nfloor(1, 200);
    fprintf(stdout,
"\n******************************************************************\n");      
fprintf(stdout, "\n**********************mball_nfloor(4, 1)**************************\n");
    mball_nfloor(4, 1);
    fprintf(stdout, "\n******************************************************************\n");    return 0;
}
//b(m,n) = min{ max{ b(m-1,k-1)+1, b(m,n-k)+1 } }
//n>=1,m>=2,1<=k<=n
//For convenience, we handle m=1, n=0 separately
void mball_nfloor(int m, int n){
    if(m<=0 || n<=0)
        return;
    int result[m+1][n+1];
    int temp_min = n+1;
    int temp_max = 0;
    //for n==0 and n==1
    for(int i=0;i<=m;i++){
        result[i][0] = 0;
        result[i][1] = 1;
    }
    //for m==0 and m==1
    for(int i=0;i<=n;i++){
        result[1][i] = i;
        result[0][i] = 0;
    }
    if(m>1 && n>1){
        //start from 2 balls
        for(int a=2;a<=m;a++){
            //start from 1 floors
            for(int i=1;i<=n;i++){
                for(int k=1;k<=i;k++){
                     temp_max = max(result[a-1][k-1]+1, result[a][i-k]+1);
                    temp_min = min(temp_min, temp_max);
                }
                result[a][i] = temp_min;
                temp_min = n+1;
            }
        }
    }
    for(int i=1;i<=m;i++){
        fprintf(stdout, "\n======%d个球,%d层,每种楼层各种丢球方法中最坏情况下的最少丢球次数======\n", i, n);
        for(int j=1;j<=n;j++){
            fprintf(stdout, "%d\t", result[i][j]);
        }
        fprintf(stdout, "\n");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值