<HDU 1722>Cake

    xiaoxiao2021-03-25  100

    Cake

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4250    Accepted Submission(s): 2136 Problem Description 一次生日Party可能有p人或者q人参加,现准备有一个大蛋糕.问最少要将蛋糕切成多少块(每块大小不一定相等),才能使p人或者q人出席的任何一种情况,都能平均将蛋糕分食.    Input 每行有两个数p和q.   Output 输出最少要将蛋糕切成多少块.   Sample Input 2 3   Sample Output 4 Hint 将蛋糕切成大小分别为1/3,1/3,1/6,1/6的四块即满足要求. 当2个人来时,每人可以吃1/3+1/6=1/2 , 1/2块。 当3个人来时,每人可以吃1/6+1/6=1/3 , 1/3, 1/3块。

    解题思路:

    自己想了好久,没想出什么规律,这也是百度得到的。

    比如 用一个矩形来切割,其实应该是圆的。这里边界也得加上,因为首尾其实是相连的

    比如4 ,6  把一个矩形切成4份,需要4刀(加上边界),,6份需要6刀

    但是有2刀是重复的,就应该把它减去。而2又是4 ,6的最小公约数。

    所以就是m+n-gcd(m,n)

    ac:

    #include <iostream> #include <stdio.h> #include <math.h> #include <string.h> #include <string> #include <algorithm> #include <stdlib.h> #include <set> #include <sstream> using namespace std; int gcd(int a,int b){ while(b){ int temp = b; b = a%b; a = temp; } return a; } int main() { int m,n; while(cin>>m>>n){ cout<<m+n-gcd(m,n)<<endl; } }

    转载请注明原文地址: https://ju.6miu.com/read-21213.html

    最新回复(0)