将N跟香肠平均分成M份的最少切割刀数
来源:互联网 发布:java 监听器 编辑:程序博客网 时间:2024/04/29 13:23
1000. Cutting Sausages
soj.me
2014.3.1
Description
Mirko has given up on the difficult coach job and switched to food tasting instead. Having skipped breakfast like a professional connoisseur, he is visiting a Croatian cured meat festival. The most renowned cook at the festival, Marijan Bajs, has prepared N equal sausages which need to be distributed to M tasters such that each taster gets a precisely equal amount. He will use his
trusted knife to cut them into pieces.
In order to elegantly divide the sausages, the number of cuts splitting individual sausages must be as small as possible. For instance, if there are two sausages and six tasters (the first test case below), it is sufficient to split each sausage into three equal parts, making a total of four cuts. On the other hand, if there are three sausages and four tasters (the second test case below), one possibility is cutting off three quarters of each sausage. Those larger parts will each go to one of the tasrers, while the fourth taster will get the three smaller pieces (quarters) left over.
Mirko wants to try the famous sausages, so he volunteered to help Bajs. Help them calculate the minimum total number of cuts needed to carry out the desired division.
Input
The first and only line of input contains two positive integers, N and M (1 ≤ N, M ≤ 100), the number of sausages and tasters, respectively.
Output
The first and only line of output must contain the required minimum number of cuts.
Sample Input
样例1:2 6样例2:3 4样例3:6 2
Sample Output
样例1:4样例2:3样例3:0
Problem Source: 2014年每周一赛第一场
分析:
题目的意思就将N根香肠平均的分成M份,并且要求切割的次数最少,求最少的次数.
1) 当N%M==0时,直接输出为0;
2)当N > M时,没人将得到一根完整的香肠,在将剩下的N%M根香肠平均分成M份,所以,令N = N % M后,直接跳到第三步;
3)当N < M时, 如果M%N==0, 则将每跟香肠切(M%N-1)刀; 否则,切(M%N)刀;
4)将切割之后,每跟香肠剩下的一小部分再进行平均分配。这时,是将N小段香肠平均分成(M%N)份。(即M=M%N)
5) 回到第一步进行循环,结束条件:当N是M的倍数时,结束循环,或者当M为0时,结束循环。
C代码:
#include<stdio.h>int main(){int N, M;scanf("%d%d",&N,&M);int ans = 0;while(N % M != 0) // 当N是M的倍数时,结束循环{if(N > M) N = N % M;int s = M%N == 0 ? M / N -1 : M / N; //当N小于M时,将N跟香肠分成M份,需要切的刀数。M = M % N;ans = ans + s * N;if(M == 0) //当M为0时,结束循环 break;}printf("%d\n",ans);}
0 0
- 将N跟香肠平均分成M份的最少切割刀数
- OpenCV中将一幅图平均分成N*M份,适合SVM+HOG训练的负样本采集
- 将n划分成最大数不超过m的划分数
- 将整数n分成k份
- 如何将一张披萨饼平均分成11份
- 将一个整数M分成N个数相加的和,要求每个数至少要大于等于1
- 将你的收入分成5份
- 给一堆数均匀分成N份--等边三角形/任务分配
- OpenCV通过设置ROI将图像平均分成N等分
- LeetCode(Palindrome partition 2) 求将一个字符串划分成回文子串 需要分成的段数最少是多少
- 整数N分成K份
- N分成m个数的和算法
- m个数分成n个数的和
- LeetCode | Palindrome Partitioning II(最少切割次数将一个子串划分成回文子串)
- 将一个数组分成和尽可能相等的两份
- 一个整数组,长度为 n,将其分为 m 份,使各的和相等求 m 的最大值,比如{3,2,4,3,6} 可以分成{3,2,4,3,6,} m=1;{3,6}{2,4,3}m=2 {3,3}{2,4}{6}
- 一个整数数组,长度为n,将其分为m 份,使各份的和相等,求m 的最大值 比如{3,2,4,3,6} 可以分成{3,2,4,3,6} m=1; {3,6}{2,4,3} m=2 {3,3}{2,4}{6
- 一个整数数组,长度为n,将其分为m份,使各份的和相等,求m 的最大值比如{3,2,4,3,6} 可以分成{3,2,4,3,6} m=1; {3,6}{2,4,3} m=2 {3,3}{2
- POJ2184:Cow Exhibition(变种01背包)
- 九度:1012<并查集>
- dojo小例子(4)DataGrid对动态数据的增删
- 经验分享二 如何选中ViewPager某个view
- 输入5个同学的姓名,将这些姓名按照字符串的大小进行升序排列并输出(C++语言)
- 将N跟香肠平均分成M份的最少切割刀数
- hibernate session的flushMode的区别
- struts2使用action方式实现文件下载(支持中文文件名)
- Eclipse中10个最有用的快捷键
- C/C++函数的调用约定(MS VC)
- Android Intent Action 大全
- python magic method
- 如果恢复搞活搞活
- 五险一金,终于懂了,你懂吗(不看以后准吃亏