HDOJ 题目4112Break the Chocolate(数学)

本文介绍了一个有趣的编程问题——如何将一块三维巧克力分割成单位大小的块,并提供了两种方法的最优解:徒手折断和使用刀具切割。通过数学分析和代码实现,展示了算法设计与分析的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



Break the Chocolate

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4066    Accepted Submission(s): 1287


Problem Description

Benjamin is going to host a party for his big promotion coming up.
Every party needs candies, chocolates and beer, and of course Benjamin has prepared some of those. But as everyone likes to party, many more people showed up than he expected. The good news is that candies are enough. And for the beer, he only needs to buy some extra cups. The only problem is the chocolate.
As Benjamin is only a 'small court officer' with poor salary even after his promotion, he can not afford to buy extra chocolate. So he decides to break the chocolate cubes into smaller pieces so that everyone can have some.
He have two methods to break the chocolate. He can pick one piece of chocolate and break it into two pieces with bare hand, or put some pieces of chocolate together on the table and cut them with a knife at one time. You can assume that the knife is long enough to cut as many pieces of chocolate as he want.
The party is coming really soon and breaking the chocolate is not an easy job. He wants to know what is the minimum number of steps to break the chocolate into unit-size pieces (cubes of size 1 × 1 × 1). He is not sure whether he can find a knife or not, so he wants to know the answer for both situations.
 

Input
The first line contains an integer T(1<= T <=10000), indicating the number of test cases.
Each test case contains one line with three integers N,M,K(1 <=N,M,K <=2000), meaning the chocolate is a cube of size N ×M × K.
 

Output
For each test case in the input, print one line: "Case #X: A B", where X is the test case number (starting with 1) , A and B are the minimum numbers of steps to break the chocolate into N × M × K unit-size pieces with bare hands and knife respectively.
 

Sample Input
  
2 1 1 3 2 2 2
 

Sample Output
  
Case #1: 2 2 Case #2: 7 3
 

Source
 

Recommend
We have carefully selected several similar problems for you:   4119  4118  4114  4115  4111 
分析:用手掰很明显是(n*m*k-1)次操作。用刀切注意不是((n-1)+(m-1)+(k-1))次操作,这只是不动原巧克力的操作数。举个例子:1*1*4的巧克力,用刀切,按上面说的要3次操作,实际上只需2次。所以不管是长宽高,都只需要[log 2 n](或m,k)次操作。
 ac代码
#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
	int t,cot=0;
	scanf("%d",&t);
	while(t--)
	{
		double a,b,c;
		double n1=0,n2=0;
		scanf("%lf%lf%lf",&a,&b,&c);
		n1=(a-1)+(b-1)*a+(c-1)*a*b;
		n2=ceil(log(a)/log(2.0))+ceil(log(b)/log(2.0))+ceil(log(c)/log(2));
		printf("Case #%d: %.lf %.lf\n",++cot,n1,n2);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值