切蛋糕问题

在这里插入图片描述

  • 假设每一刀沿着半径切,那么切k刀就有k块。
  • 要想切出的块数最少,必须保证重合刀数最多。
  • 重合刀数为gcd(p,q),因为在切了k = gcd(p,q)刀后,再切x刀(x * k= p)或者y刀(y * k = q),都不会有重合。
  • 综上,切的总刀数(或说块数)为(p + q - gcd(p,q))。
#include<iostream>
#include<algorithm>
using namespace std;

int gcd(int a,int b)
{
    
    
	return b == 0 ? a : gcd(b, a % b);
}

int main()
{
    
    
	int p,q;
	while(cin >> p >> q)
	{
    
    
		cout << p + q - gcd(p,q) << endl;
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/weixin_45688536/article/details/105688114
今日推荐