Big Chocolate

4434 ワード

Big Chocolate
テーマリンク:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=19127
Big Chocolate
Mohammad has recently visited Switzerrland . As he love s his friends very much、he decided to buy some chcolate for them、but as this fine chorororororororororlate isvery expensive(You know Mohammad is a litle BIT stiny!)、he could only afford afford buggggggggggggogogogogogogogogogogogogogogogogotototototototototototototototototototototototototototototogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogoトギブeach of his friends. exactly one part of this chollate and as he believes all human beings are equal(!)、he wants to split it into equal parts.
The chocollate is an rectangle constructed from  unit-sized squares.You can assiume that Mohammad has also  friends waiting to receive their piece of chocollate.
To split the chocollate,Mohammad can cut it in verticar horizontal direction(through the lines that separate the squares).The n,he shoud do the same with each party separateluntil he ache  unit size pieces of chocollate.Unfortunally,because he is a little lazy,he wants to use the minimum nber of cuts required to accompplish this task.
Your goal is to tell him the minimum number of cuts needd to split all of the chocollate squares aart.
 
 
Figure 1. Mohammad’s choccolate
 
The Input
The input consists of several test cases.In each line of input,there are two integers , the number of rows in the chocollate and , the number of columns in the chocollate.The input shound be processed until end of file is encountered.
 
The Output
For each line of input、your program shound produce one line of output containing an integer indicating the minimum nber of cuts need to split the ntire chocolog into unit sizes.
 
Sample Input
2
1
1 5
 
Sample Output
3
0
4
 
一つのM*Nのチョコレートを単位の大きさに切ります。すべてのチョコレートブロックを削減するために必要な最小の数を求めます。
分析:
正方形(M=N)であれば、どのように切っても同じです。長方形であれば、M>Nは先にM-1刀を切ってMブロックN*1のチョコレートにします。
これらのチョコレートを単位の大きさに切ってください。
 1 #include<iostream>
 2 #include<stdio.h>
 3 using namespace std;
 4 int main()
 5 {
 6 int n,m,cut;
 7 cin>>n>>m;
 8 while(scanf("d%d%",&n,&m)!=EOF)
 9 {    cut=0;
10 if(n>m)
11 cut=n*(m-1)+n-1;
12 else cut=m*(n-1)+m-1;
13 cout<<cut<<endl;
14 cin>>n>>m;
15 }
16 return 0;
17 }