将N跟香肠平均分成M份的最少切割刀数

1000. Cutting Sausages
soj.me
2014.3.1
 
     
 
Time Limit: 1sec    Memory Limit:256MB
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
 Copy sample input to clipboard
样例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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值