问题:箱子体积除以166的值为体积重量,快递公司希望将体积重量向上取整(体积166时是1,167时是2),输入长宽高,输出向上取整后的体积重量。
答案:
// Code file created by C Code Develop
#include "stdio.h"
#include "stdlib.h"
int main(viod){
int weight, volume, l, k,g;
scanf("%d", &l);
scanf("%d", &k);
scanf("%d", &g);
volume = l*k*g;
weight = (volume+165)/166;
printf("%d",weight);
return 0;
}
关键是不能直接除以166再加1,因为166时这样就为2了,把(volume+165)再除以166时可以实现向上取整。
或者
#include "stdio.h"
#include "stdlib.h"
int main(){
int weight, volume, l, k, g;
scanf("%d", &l);
scanf("%d", &k);
scanf("%d", &g);
volume = l*k*g;
if(volume % 166)
printf("%d", volume/166+1);
else
printf("%d", volume/166);
return 0;
}
分类讨论即可