给朋友分巧克力

(慢慢看题目,O(∩_∩)O~)

Mohammad has recently visited Switzerland . As he loves his friends very much, he decided to buy some chocolate for them, but as this fine chocolate is very expensive(You know Mohammad is a little BIT stingy!), he could only afford buying one chocolate, albeit a very big one (part of it can be seen in figure 1) for all of them as a souvenir. Now, he wants to give each of his friends exactly one part of this chocolate and as he believes all human beings are equal (!), he wants to split it into equal parts.

The chocolate is an rectangle constructed from  unit-sized squares. You can assume that Mohammad has also  friends waiting to receive their piece of chocolate.

To split the chocolate, Mohammad can cut it in vertical or horizontal direction (through the lines that separate the squares). Then, he should do the same with each part separately until he reaches  unit size pieces of chocolate. Unfortunately, because he is a little lazy, he wants to use the minimum number of cuts required to accomplish this task.

Your goal is to tell him the minimum number of cuts needed to split all of the chocolate squares apart.

Figure 1. Mohammad’s chocolate

The Input

The input consists of several test cases. In each line of input, there are two integers , the number of rows in the chocolate and , the number of columns in the chocolate. The input should be processed until end of file is encountered.

The Output

For each line of input, your program should produce one line of output containing an integer indicating the minimum number of cuts needed to split the entire chocolate into unit size pieces.

Sample Input

2 2

1 1

1 5

Sample Output

3

0

4

题目分析:
     通过对案例分析,分巧克力一次只能切一大块连着的,所以要切的次数刚好的rows*columns-1.

源代码:

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5         int N, M, sum = 0;               //定义行N,列M。
 6         while (cin >> N >> M)
 7         {
 8             sum = N*M;
 9             cout << sum - 1 << endl;
10         }
11
12     return 0;
13 }

心得:

在VS的环境编程要加一句system("pause")暂停来看输入内容。有时候题目长不一定难做,仔细分析,慢慢就能得到思路。虽然在第一次比赛中没有拿到好名次,但是每天能学到东西,积累知识,就是一种收获,自己平时做得题目也没别人多,我想这是比赛做题目不顺手的主要原因吧!加油!(^ω^)

时间: 2024-07-28 21:29:23

给朋友分巧克力的相关文章

ACM分巧克力问题

Description Mohammad has recently visited Switzerland . As he loves his friends very much, he decided to buy some chocolate for them, but as this fine chocolate is very expensive(You know Mohammad is a little BIT stingy!), he could only afford buying

第八届蓝桥杯省赛 分巧克力

问题描述 儿童节那天有K位小朋友到小明家做客.小明拿出了珍藏的巧克力招待小朋友们. 小明一共有N块巧克力,其中第i块是Hi x Wi的方格组成的长方形. 为了公平起见,小明需要从这N块巧克力中切出K块巧克力分给小朋友们.切出的巧克力需要满足: 1. 形状是正方形,边长是整数 2. 大小相同 例如一块6x5的巧克力可以切出6块2x2的巧克力或者2块3x3的巧克力. 当然小朋友们都希望得到的巧克力尽可能大,你能帮小Hi计算出最大的边长是多少么?输入格式 第一行包含两个整数N和K.(1 <= N, K

CCF - 201703-1 - 分蛋糕

问题描述 试题编号:    201703-1试题名称:    分蛋糕时间限制:    1.0s内存限制:    256.0MB问题描述:    问题描述 小明今天生日,他有n块蛋糕要分给朋友们吃,这n块蛋糕(编号为1到n)的重量分别为a1, a2, -, an.小明想分给每个朋友至少重量为k的蛋糕.小明的朋友们已经排好队准备领蛋糕,对于每个朋友,小明总是先将自己手中编号最小的蛋糕分给他,当这个朋友所分得蛋糕的重量不到k时,再继续将剩下的蛋糕中编号最小的给他,直到小明的蛋糕分完或者这个朋友分到的蛋

分蛋糕

问题描述 试题编号: 201703-1 试题名称: 分蛋糕 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 小明今天生日,他有n块蛋糕要分给朋友们吃,这n块蛋糕(编号为1到n)的重量分别为a1, a2, -, an.小明想分给每个朋友至少重量为k的蛋糕.小明的朋友们已经排好队准备领蛋糕,对于每个朋友,小明总是先将自己手中编号最小的蛋糕分给他,当这个朋友所分得蛋糕的重量不到k时,再继续将剩下的蛋糕中编号最小的给他,直到小明的蛋糕分完或者这个朋友分到的蛋糕的总重量大于等于k.

CCF201703-1 分蛋糕

试题编号:    201703-1试题名称:    分蛋糕时间限制:    1.0s内存限制:    256.0MB问题描述:    问题描述 小明今天生日,他有n块蛋糕要分给朋友们吃,这n块蛋糕(编号为1到n)的重量分别为a1, a2, -, an.小明想分给每个朋友至少重量为k的蛋糕.小明的朋友们已经排好队准备领蛋糕,对于每个朋友,小明总是先将自己手中编号最小的蛋糕分给他,当这个朋友所分得蛋糕的重量不到k时,再继续将剩下的蛋糕中编号最小的给他,直到小明的蛋糕分完或者这个朋友分到的蛋糕的总重量

CSP201703-1:分蛋糕

引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试,针对计算机软件开发.软件测试.信息管理等领域的专业人士进行能力认证.认证对象是从事或将要从事IT领域专业技术与技术管理人员,以及高校招考研究生的复试对象. 问题描述 小明今天生日,他有n块蛋糕要分给朋友们吃,这n块蛋糕(编号为1到n)的重量分别为a1, a2, -, an.小明想分给每个朋友至少重量为k的蛋糕

201903-1 分蛋糕

试题编号: 201703-1 试题名称: 分蛋糕 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 小明今天生日,他有n块蛋糕要分给朋友们吃,这n块蛋糕(编号为1到n)的重量分别为a1, a2, -, an.小明想分给每个朋友至少重量为k的蛋糕.小明的朋友们已经排好队准备领蛋糕,对于每个朋友,小明总是先将自己手中编号最小的蛋糕分给他,当这个朋友所分得蛋糕的重量不到k时,再继续将剩下的蛋糕中编号最小的给他,直到小明的蛋糕分完或者这个朋友分到的蛋糕的总重量大于等于k. 请问当小

CCF——分蛋糕(2017-3)

问题描述 小明今天生日,他有n块蛋糕要分给朋友们吃,这n块蛋糕(编号为1到n)的重量分别为a1, a2, -, an.小明想分给每个朋友至少重量为k的蛋糕.小明的朋友们已经排好队准备领蛋糕,对于每个朋友,小明总是先将自己手中编号最小的蛋糕分给他,当这个朋友所分得蛋糕的重量不到k时,再继续将剩下的蛋糕中编号最小的给他,直到小明的蛋糕分完或者这个朋友分到的蛋糕的总重量大于等于k. 请问当小明的蛋糕分完时,总共有多少个朋友分到了蛋糕. 输入格式 输入的第一行包含了两个整数n, k,意义如上所述. 第二

RAMPS1.4 3D打印控制板:软件下载\连接\安装\测试

RAMPS1.4 3D打印控制板:软件下载\连接\安装\测试 特别说明: 电源接反,电机驱动板接反将有可能烧毁芯片和电路,请再三确认后再进行通电. 如何使用: 1.需要用到的模块或器件: Arduino Mega2560 主控板(该套件包含):这是3d打印机的大脑,负责控制整个打印机来完成特定的动作,如打印特定的文件等: RAMPS1.4 (该套件包含):这是上面主控板的拓展板,有它是为了更好的与其它硬件进行连接和控制的,起到过渡桥梁的作用: A4988步进电机驱动板(该套件包含):这是用来连接